001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.naming.lib;
018:
019: import org.objectweb.fractal.api.control.BindingController;
020: import org.objectweb.fractal.api.control.IllegalLifeCycleException;
021: import org.objectweb.fractal.api.control.LifeCycleController;
022: import org.objectweb.jorm.api.PException;
023: import org.objectweb.jorm.api.PMapper;
024: import org.objectweb.jorm.naming.api.PName;
025: import org.objectweb.jorm.naming.api.PNameCoder;
026: import org.objectweb.perseus.cache.api.CacheManager;
027: import org.objectweb.speedo.api.SpeedoException;
028: import org.objectweb.speedo.api.SpeedoProperties;
029: import org.objectweb.speedo.mapper.api.JormFactory;
030: import org.objectweb.speedo.metadata.SpeedoClass;
031: import org.objectweb.speedo.naming.api.NamingManager;
032: import org.objectweb.speedo.naming.api.NamingManagerFactoryItf;
033: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
034: import org.objectweb.util.monolog.api.BasicLevel;
035: import org.objectweb.util.monolog.api.Logger;
036:
037: import java.util.Arrays;
038:
039: /**
040: * <img src="../api/NamingManagerFactory.gif"/>
041: * This small fractal component is in charge of the naming specialization in
042: * Speedo. This component is a factory of NamingManager.
043: * Each NamingManager manages a type of naming. Currently Speedo provides the
044: * following naming manager implementations:
045: * - UserIdCompositeNamingManager
046: * - UserIdSingleNamingManager
047: * - LongIdNamingManager
048: * - OLongIdNamingManager
049: * - RdbSequenceNamingManager
050: * The NamingManagerFactory has 3 dependencies:
051: * - The CacheManager permits for some PNamingContext to lookup persistent
052: * object in the cache before doing I/O (polymorphism case for instance).
053: * - The POManagerFactory permits to fetch the SequenceManager associated to
054: * the POMF.
055: * - The PMapper permits to NamingManager to use some internal persistent
056: * classes (ex generator).
057: *
058: * @see org.objectweb.speedo.naming.api.NamingManager
059: * @see org.objectweb.speedo.naming.lib.UserIdSingleNamingManager
060: * @see org.objectweb.speedo.naming.lib.UserIdCompositeNamingManager
061: * @see org.objectweb.speedo.naming.lib.RdbSequenceNamingManager
062: * @see org.objectweb.speedo.naming.lib.LongIdNamingManager
063: * @see org.objectweb.speedo.naming.lib.OLongIdNamingManager
064: * @see org.objectweb.speedo.naming.lib.PolymorphIdNamingManager
065: * @author S.Chassande-Barrioz
066: */
067: public class NamingManagerFactory implements NamingManagerFactoryItf,
068: BindingController, LifeCycleController {
069:
070: /**
071: * fractal binding name to the mapper
072: */
073: public final static String MAPPER_BINDING = "mapper";
074: /**
075: * fractal binding name to the cache manager
076: */
077: public final static String CACHE_MANAGER_BINDING = "cache-manager";
078: public final static String PMF_BINDING = "po-manager-factory";
079:
080: private NamingManager[] namingManagers;
081:
082: private PMapper mapper;
083: private CacheManager cache;
084: private POManagerFactoryItf pmf;
085: private Logger logger;
086:
087: public NamingManagerFactory() {
088: init();
089: }
090:
091: private void init() {
092: namingManagers = new NamingManager[] {
093: new UserIdSingleNamingManager(),
094: new RdbSequenceNamingManager(),
095: new UserIdCompositeNamingManager(),
096: new LongIdNamingManager(), new OLongIdNamingManager(),
097: new PolymorphIdNamingManager() };
098: }
099:
100: public PMapper getMapper() {
101: return mapper;
102: }
103:
104: public void setMapper(PMapper mapper) {
105: this .mapper = mapper;
106: if (started) {
107: for (int i = 0; i < namingManagers.length; i++) {
108: try {
109: namingManagers[i].setPMapper(mapper);
110: } catch (PException e) {
111: if (logger != null) {
112: logger
113: .log(
114: BasicLevel.WARN,
115: "The naming manager "
116: + namingManagers[i]
117: + " does not accept the mapper: ",
118: e);
119: }
120: }
121: }
122: }
123: }
124:
125: public CacheManager getCache() {
126: return cache;
127: }
128:
129: public void setCache(CacheManager cache) {
130: this .cache = cache;
131: if (started) {
132: for (int i = 0; i < namingManagers.length; i++) {
133: namingManagers[i].setCache(cache);
134: }
135: }
136: }
137:
138: // IMPLEMENTATION OF THE UserBindingController INTERFACE //
139: //-------------------------------------------------------//
140:
141: public String[] listFc() {
142: return new String[] { MAPPER_BINDING, CACHE_MANAGER_BINDING,
143: PMF_BINDING };
144: }
145:
146: public Object lookupFc(String s) {
147: if (MAPPER_BINDING.equals(s))
148: return mapper;
149: else if (CACHE_MANAGER_BINDING.equals(s))
150: return cache;
151: else if (PMF_BINDING.equals(s))
152: return pmf;
153: else
154: return null;
155: }
156:
157: public void bindFc(String s, Object o) {
158: if ("logger".equals(s)) {
159: setLogger((Logger) o);
160: } else if (MAPPER_BINDING.equals(s)) {
161: setMapper((PMapper) o);
162: } else if (CACHE_MANAGER_BINDING.equals(s)) {
163: setCache((CacheManager) o);
164: } else if (PMF_BINDING.equals(s)) {
165: setPmf((POManagerFactoryItf) o);
166: }
167: }
168:
169: public void unbindFc(String s) {
170: if (MAPPER_BINDING.equals(s)) {
171: setMapper(null);
172: } else if (CACHE_MANAGER_BINDING.equals(s)) {
173: setCache(null);
174: } else if (PMF_BINDING.equals(s)) {
175: setPmf(null);
176: }
177: }
178:
179: private boolean started = false;
180:
181: public String getFcState() {
182: return started ? STARTED : STOPPED;
183: }
184:
185: public void startFc() throws IllegalLifeCycleException {
186: if (!started) {
187: started = true;
188: setPmf(pmf);
189: setMapper(mapper);
190: setCache(cache);
191: }
192: }
193:
194: public void stopFc() throws IllegalLifeCycleException {
195: if (started) {
196: started = false;
197: }
198: }
199:
200: public POManagerFactoryItf getPmf() {
201: return pmf;
202: }
203:
204: public void setPmf(POManagerFactoryItf pmf) {
205: this .pmf = pmf;
206: for (int i = 0; i < namingManagers.length; i++) {
207: namingManagers[i].setPmf(pmf);
208: }
209: }
210:
211: public Logger getLogger() {
212: return logger;
213: }
214:
215: public void setLogger(Logger logger) {
216: this .logger = logger;
217: for (int i = 0; i < namingManagers.length; i++) {
218: namingManagers[i].setLogger(logger);
219: }
220: }
221:
222: public synchronized void bindNamingManager(NamingManager nm) {
223: NamingManager[] neo = new NamingManager[namingManagers.length + 1];
224: System.arraycopy(namingManagers, 0, neo, 0,
225: namingManagers.length);
226: neo[namingManagers.length] = nm;
227: namingManagers = neo;
228: }
229:
230: public synchronized boolean unbindNamingManager(NamingManager nm) {
231: int idx = Arrays.binarySearch(namingManagers, nm);
232: if (idx >= 0) {
233: //put the last in place of the removed
234: namingManagers[idx] = namingManagers[namingManagers.length - 1];
235: //create a array without the lastest element
236: NamingManager[] neo = new NamingManager[namingManagers.length - 1];
237: System.arraycopy(namingManagers, 0, neo, 0,
238: namingManagers.length - 1);
239: namingManagers = neo;
240: return true;
241: } else {
242: return false;
243: }
244: }
245:
246: public NamingManager getNamingManager(SpeedoClass sc)
247: throws SpeedoException {
248: for (int i = 0; i < namingManagers.length; i++) {
249: if (namingManagers[i].canManage(sc)) {
250: return namingManagers[i];
251: }
252: }
253: throw new SpeedoException(
254: "No identity manager found for the class '"
255: + sc.getFQName()
256: + "', "
257: + sc.getIdentityType()
258: + ", "
259: + sc.getExtension(SpeedoProperties.VENDOR_NAME,
260: SpeedoProperties.ID));
261: }
262:
263: public NamingManager getNamingManager(String hints,
264: ClassLoader classloader) throws PException {
265: for (int i = 0; i < namingManagers.length; i++) {
266: if (namingManagers[i].canProvidePBinder(hints, classloader)) {
267: return namingManagers[i];
268: }
269: }
270: throw new PException(
271: "No identity manager found for the hints '" + hints
272: + "'.");
273: }
274:
275: public PName decode(PNameCoder pnc, Object oid,
276: java.lang.Class clazz, JormFactory jf) throws PException {
277: for (int i = 0; i < namingManagers.length; i++) {
278: PName pn = namingManagers[i].decode(pnc, oid, clazz, jf);
279: if (pn != null) {
280: return pn;
281: }
282: }
283: throw new PException("No identity manager able to decode:"
284: + "\n\tpnc=" + pnc + "\n\toid=" + oid + "\n\tclazz="
285: + clazz);
286: }
287:
288: public Object encode(PName pn) throws PException {
289: for (int i = 0; i < namingManagers.length; i++) {
290: Object o = namingManagers[i].encode(pn);
291: if (o != null) {
292: return o;
293: }
294: }
295: throw new PException("No identity manager able to encode ("
296: + pn + ")");
297: }
298:
299: /**
300: * Clean the nmf.
301: */
302: public void clean() {
303: init();
304: }
305: }
|