View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.dynabean;
30  
31  import java.util.Collections;
32  import java.util.Set;
33  import java.util.WeakHashMap;
34  
35  import org.apache.commons.beanutils.DynaBean;
36  import org.apache.commons.beanutils.DynaClass;
37  import org.apache.commons.beanutils.DynaProperty;
38  import org.openrdf.elmo.Entity;
39  import org.openrdf.elmo.annotations.rdf;
40  import org.openrdf.elmo.dynabean.helpers.DynaClassInfo;
41  import org.openrdf.elmo.dynabean.helpers.DynaClassInfoFactory;
42  import org.openrdf.elmo.exceptions.ElmoPersistException;
43  import org.openrdf.elmo.sesame.SesameManager;
44  import org.openrdf.elmo.sesame.roles.SesameEntity;
45  import org.openrdf.model.BNode;
46  import org.openrdf.model.URI;
47  import org.openrdf.model.ValueFactory;
48  import org.openrdf.model.vocabulary.RDF;
49  import org.openrdf.query.QueryEvaluationException;
50  import org.openrdf.repository.Repository;
51  import org.openrdf.repository.RepositoryException;
52  import org.openrdf.repository.contextaware.ContextAwareConnection;
53  
54  /**
55   * DynaClass interface for Elmo JavaBeans. This class should be used to create
56   * new DynaBean.
57   * 
58   * @author James Leigh
59   * @see RdfDynaClass
60   * @see DynaBeanSupport
61   * 
62   */
63  @SuppressWarnings("unchecked")
64  @rdf("http://www.w3.org/2000/01/rdf-schema#Class")
65  public class DynaClassSupport implements DynaClass {
66  
67  	private static WeakHashMap<Repository, DynaClassInfoFactory> factories = new WeakHashMap();
68  
69  	private SesameManager manager;
70  
71  	private DynaClassInfo dynaClassInfo;
72  
73  	public DynaClassSupport(Entity elmo) {
74  		SesameEntity bean = (SesameEntity) elmo;
75  		this.manager = bean.getSesameManager();
76  		DynaClassInfoFactory factory;
77  		Repository repository = manager.getConnection().getRepository();
78  		synchronized (factories) {
79  			factory = factories.get(repository);
80  			if (factory == null) {
81  				factory = new DynaClassInfoFactory();
82  				factories.put(repository, factory);
83  			}
84  		}
85  		try {
86  			URI uri = (URI) bean.getSesameResource();
87  			Set<URI> singleton = Collections.singleton(uri);
88  			this.dynaClassInfo = factory.buildDynaClass(manager, singleton);
89  		} catch (RepositoryException e) {
90  			throw new ElmoPersistException(e);
91  		} catch (QueryEvaluationException e) {
92  			throw new ElmoPersistException(e);
93  		}
94  	}
95  
96  	public void setDynaClassInfo(DynaClassInfo dynaClassInfo) {
97  		this.dynaClassInfo = dynaClassInfo;
98  	}
99  
100 	public DynaProperty[] getDynaProperties() {
101 		return dynaClassInfo.getDynaProperties();
102 	}
103 
104 	public DynaProperty getDynaProperty(String name) {
105 		return dynaClassInfo.getDynaProperty(name);
106 	}
107 
108 	public String getName() {
109 		return dynaClassInfo.toString();
110 	}
111 
112 	public DynaBean newInstance() throws IllegalAccessException,
113 			InstantiationException {
114 		ContextAwareConnection conn = manager.getConnection();
115 		ValueFactory vf = conn.getRepository().getValueFactory();
116 		BNode bNode = vf.createBNode();
117 		try {
118 			for (URI rdfType : dynaClassInfo.getRdfTypes()) {
119 				conn.add(bNode, RDF.TYPE, rdfType);
120 			}
121 		} catch (RepositoryException e) {
122 			throw new ElmoPersistException(e);
123 		}
124 		return (DynaBean) manager.find(bNode);
125 	}
126 
127 }