KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > archive > ArchiveManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ArchiveManager.java 953 2006-07-26 17:03:18Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.archive;
27
28 import java.lang.reflect.ParameterizedType JavaDoc;
29 import java.lang.reflect.Type JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.objectweb.easybeans.api.EZBArchive;
34 import org.objectweb.easybeans.api.EZBArchiveFactory;
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37
38 /**
39  * Creates an archive for the given type.
40  * @author Florent Benoit
41  */

42 public final class ArchiveManager {
43
44     /**
45      * Logger.
46      */

47     private static JLog logger = JLogFactory.getLog(ArchiveManager.class);
48
49     /**
50      * Unique instance of this class.
51      */

52     private static ArchiveManager archiveManager;
53
54     /**
55      * Mapping between class and the associated factories.<br />
56      * ie : File <--> DefaultArchiveFactory.
57      */

58     private Map JavaDoc<Class JavaDoc, EZBArchiveFactory> factories = new HashMap JavaDoc<Class JavaDoc, EZBArchiveFactory>();
59
60     /**
61      * Private constructor as only one instance is built.
62      */

63     private ArchiveManager() {
64         addFactory(new DefaultArchiveFactory());
65     }
66
67     /**
68      * Gets the unique instance of this class.
69      * @return the unique instance.
70      */

71     public static ArchiveManager getInstance() {
72         if (archiveManager == null) {
73             archiveManager = new ArchiveManager();
74         }
75         return archiveManager;
76     }
77
78     /**
79      * Adds the given factory on this manager. It will look the generic info
80      * used by this class to find the class object managed by this factory.
81      * @param factory the factory to add.
82      */

83     public void addFactory(final EZBArchiveFactory factory) {
84
85         Class JavaDoc factoryClass = factory.getClass();
86         Class JavaDoc argumentClass = getSupportedType(factoryClass);
87
88         logger.debug("Adding factory '" + factoryClass.getName() + "' for class '" + argumentClass + "'.");
89
90         // Add factory
91
factories.put(argumentClass, factory);
92
93     }
94
95     /**
96      * Determines the type supported by the given factory Class object.
97      * It will look the generic info used by this class to find the
98      * class object managed by this factory.
99      * @param factoryClass EZBArchiveFactory sub Class.
100      * @return Returns the type supported by the given factory Class object.
101      */

102     private Class JavaDoc getSupportedType(final Class JavaDoc factoryClass) {
103
104         // Get generics of this class
105
Type JavaDoc[] generics = factoryClass.getGenericInterfaces();
106
107         // size = 1
108
if (generics.length != 1) {
109             throw new IllegalArgumentException JavaDoc("Invalid class, supports only class with a single generic");
110         }
111
112         // parametrized type ?
113
Type JavaDoc factoryClassType = generics[0];
114         ParameterizedType JavaDoc parameterType = null;
115
116         if (factoryClassType instanceof ParameterizedType JavaDoc) {
117             parameterType = (ParameterizedType JavaDoc) factoryClassType;
118         } else {
119             throw new IllegalArgumentException JavaDoc("Object '" + factoryClassType + "' is not a ParameterizedType");
120         }
121
122         Type JavaDoc[] arguments = parameterType.getActualTypeArguments();
123         if (arguments.length != 1) {
124             throw new IllegalArgumentException JavaDoc("Invalid class, supports only class with a single generic");
125         }
126         return (Class JavaDoc) arguments[0];
127     }
128
129     /**
130      * Removes the given factory on this manager. It will look the generic info
131      * used by this class to find the class object managed by this factory.
132      * @param factory the factory to remove.
133      */

134     public void removeFactory(final EZBArchiveFactory factory) {
135
136         Class JavaDoc factoryClass = factory.getClass();
137         Class JavaDoc argumentClass = getSupportedType(factoryClass);
138
139         if (factories.containsKey(argumentClass)) {
140             logger.debug("Removing factory '" + factoryClass.getName() + "' for class '" + argumentClass + "'.");
141
142             // Remove factory
143
factories.remove(argumentClass);
144         } else {
145             logger.debug("Factory '" + factoryClass.getName() + "' was not registered in the ArchiveManager.");
146         }
147     }
148
149     /**
150      * Creates an EZBArchive implementation object for the given object.
151      * @param o object to wrap into an EZBArchive.
152      * @return the created archive.
153      */

154     @SuppressWarnings JavaDoc("unchecked")
155     public EZBArchive getArchive(final Object JavaDoc o) {
156         // try to see if there is a matching factory
157
Class JavaDoc objectClass = o.getClass();
158
159         // Factory ?
160
EZBArchiveFactory factory = factories.get(objectClass);
161
162         if (factory == null) {
163
164             // Iterates over the registered classes and find the assignable class
165
for (Class JavaDoc cls : factories.keySet()) {
166                 if (cls.isAssignableFrom(objectClass) && factory == null) {
167                     // we found a match
168
factory = factories.get(cls);
169                 }
170             }
171
172             // if after all, factory is still null, raise an Exception
173
if (factory == null) {
174                 throw new IllegalArgumentException JavaDoc("No factory found for the type '" + objectClass + "'.");
175             }
176         }
177
178         return factory.create(o);
179     }
180
181 }
182
Popular Tags