View Javadoc

1   /*
2   * E-nspire Gemini.
3   * A Java and AspectJ based framework that enables transparent 
4   * bidirectional relationships between Plain Old Java Objects.
5   * 
6   * Copyright (C) 2005 Dragan Djuric
7   * 
8   * This program is free software; you can redistribute it and/or
9   * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  * 
22  * Contact the author at dragand@dev.java.net
23  */
24  package com.enspire.gemini.context;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import com.enspire.context.Factory;
30  import com.enspire.context.impl.AliasFactory;
31  import com.enspire.context.impl.ExistingInstanceFactory;
32  import com.enspire.context.impl.NewInstanceFactory;
33  import com.enspire.gemini.RelationshipUpdater;
34  import com.enspire.gemini.updaters.CollectionPropertyRelationshipUpdater;
35  import com.enspire.gemini.updaters.SimplePropertyRelationshipUpdater;
36  import com.enspire.reflection.PropertyReflection;
37  import com.enspire.reflection.beanutils.PropertyUtilsPropertyReflection;
38  
39  /***
40   * This <code>Factory</code> sets up the context of Gemini. In other words, it
41   * injects the right dependencies into Gemini objects.
42   * 
43   * @author Dragan Djuric <dragand@dev.java.net>
44   * @since 1.0
45   */
46  public class GeminiFactory implements Factory {
47  
48      private Factory decorated;
49      
50      /***
51       * Constructs the factory with the created context.
52       */
53      public GeminiFactory() {
54          super();
55          createContext();
56      }
57  
58      /***
59       * @see com.enspire.context.Factory#create(java.lang.Class)
60       */
61      public Object create(Class type) {
62          return decorated.create(type);
63      }
64  
65      /***
66       * @see com.enspire.context.Factory#create(java.lang.String)
67       */
68      public Object create(String name) {
69          return decorated.create(name);
70      }
71      
72      /***
73       * Creates the context of Gemini.
74       */
75      private void createContext() {
76          // Bidirectional properties
77          Map bidirectionalPropertyAliases = new HashMap(3);
78          bidirectionalPropertyAliases.put("java.util.Collection", 
79                  "com.enspire.gemini.bidirectional.BidirectionalCollection");
80          bidirectionalPropertyAliases.put("java.util.List", 
81                  "com.enspire.gemini.bidirectional.BidirectionalList");
82          bidirectionalPropertyAliases.put("java.util.Set", 
83                  "com.enspire.gemini.bidirectional.BidirectionalSet");
84          Factory bidirectionalPropertiesFactory = new AliasFactory(
85                  new NewInstanceFactory(), bidirectionalPropertyAliases);
86          
87          //PropertyReflection
88          PropertyReflection propertyReflection = 
89                  new PropertyUtilsPropertyReflection();
90          
91          //Relationship updaters
92          RelationshipUpdater simpleRelationshipUpdater = 
93                  new SimplePropertyRelationshipUpdater(propertyReflection);
94          RelationshipUpdater collectionRelationshipUpdater = 
95              new CollectionPropertyRelationshipUpdater(propertyReflection);
96          
97          Map relationshipUpdaters = new HashMap(2);
98          relationshipUpdaters.put(
99                  "com.enspire.gemini.annotation.BidirectionalOne",
100                 simpleRelationshipUpdater);
101         relationshipUpdaters.put(
102                 "com.enspire.gemini.annotation.BidirectionalMany",
103                 collectionRelationshipUpdater);
104         
105         Factory relationshipUpdatersFactory = 
106                 new ExistingInstanceFactory(relationshipUpdaters);
107         
108         //Decorated factory
109         Map products = new HashMap(3);
110         products.put("com.enspire.gemini.updaters.Factory", 
111                 relationshipUpdatersFactory);
112         products.put("com.enspire.gemini.bidirectional.Factory", 
113                 bidirectionalPropertiesFactory);
114         products.put(PropertyReflection.class.getName(), propertyReflection);
115         decorated = new ExistingInstanceFactory(products);
116     }
117 
118 }