KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > naming > lib > NamingManagerFactory


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.naming.lib;
19
20 import org.objectweb.perseus.cache.api.CacheManager;
21 import org.objectweb.speedo.naming.api.NamingManager;
22 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
23 import org.objectweb.speedo.metadata.SpeedoClass;
24 import org.objectweb.speedo.api.SpeedoException;
25 import org.objectweb.speedo.mapper.api.JormFactory;
26 import org.objectweb.speedo.api.SpeedoProperties;
27 import org.objectweb.jorm.api.PException;
28 import org.objectweb.jorm.api.PMapper;
29 import org.objectweb.jorm.naming.api.PName;
30 import org.objectweb.jorm.naming.api.PNameCoder;
31 import org.objectweb.util.monolog.api.Logger;
32 import org.objectweb.util.monolog.api.BasicLevel;
33
34 import java.util.Arrays JavaDoc;
35
36 /**
37  *
38  * @author S.Chassande-Barrioz
39  */

40 public class NamingManagerFactory {
41
42     private NamingManager[] namingManagers;
43
44     private PMapper mapper;
45     private CacheManager cache;
46     private ProxyManagerFactory pmf;
47     private Logger logger;
48
49
50     public NamingManagerFactory() {
51         namingManagers = new NamingManager[]{
52             new UserIdSingleNamingManager(),
53             new PolymorphIdNamingManager(),
54             new RdbSequenceNamingManager(),
55             new UserIdCompositeNamingManager(),
56             new LongIdNamingManager(),
57             new OLongIdNamingManager()
58         };
59     }
60
61     public PMapper getMapper() {
62         return mapper;
63     }
64
65     public void setMapper(PMapper mapper) {
66         this.mapper = mapper;
67         for(int i=0; i<namingManagers.length; i++) {
68             try {
69                 namingManagers[i].setPMapper(mapper);
70             } catch (PException e) {
71                 if (logger != null) {
72                     logger.log(BasicLevel.WARN, "The naming manager "
73                         + namingManagers[i] + " does not accept the mapper: ", e);
74                 }
75             }
76         }
77     }
78
79     public CacheManager getCache() {
80         return cache;
81     }
82
83     public void setCache(CacheManager cache) {
84         this.cache = cache;
85         for(int i=0; i<namingManagers.length; i++) {
86             namingManagers[i].setCache(cache);
87         }
88     }
89     
90     public ProxyManagerFactory getPmf() {
91         return pmf;
92     }
93     
94     public void setPmf(ProxyManagerFactory pmf) {
95         this.pmf = pmf;
96         for(int i=0; i<namingManagers.length; i++) {
97             namingManagers[i].setPmf(pmf);
98         }
99     }
100     
101     public Logger getLogger() {
102         return logger;
103     }
104
105     public void setLogger(Logger logger) {
106         this.logger = logger;
107         for(int i=0; i<namingManagers.length; i++) {
108             namingManagers[i].setLogger(logger);
109         }
110     }
111
112     public synchronized void bindNamingManager(NamingManager nm) {
113         NamingManager[] neo = new NamingManager[namingManagers.length + 1];
114         System.arraycopy(namingManagers, 0, neo, 0, namingManagers.length);
115         neo[namingManagers.length] = nm;
116         namingManagers = neo;
117     }
118
119     public synchronized boolean unbindNamingManager(NamingManager nm) {
120         int idx = Arrays.binarySearch(namingManagers, nm);
121         if (idx >= 0) {
122             //put the last in place of the removed
123
namingManagers[idx] = namingManagers[namingManagers.length - 1];
124             //create a array without the lastest element
125
NamingManager[] neo = new NamingManager[namingManagers.length - 1];
126             System.arraycopy(namingManagers, 0, neo, 0, namingManagers.length - 1);
127             namingManagers = neo;
128             return true;
129         } else {
130             return false;
131         }
132     }
133
134     public NamingManager getNamingManager(SpeedoClass sc) throws SpeedoException {
135         for(int i=0; i<namingManagers.length; i++) {
136             if (namingManagers[i].canManage(sc)) {
137                 return namingManagers[i];
138             }
139         }
140         throw new SpeedoException("No identity manager found for the class '"
141                 + sc.getFQName() + "', " + sc.identityType + ", " +
142                 sc.getExtension(SpeedoProperties.VENDOR_NAME, SpeedoProperties.ID));
143     }
144
145     public NamingManager getNamingManager(String JavaDoc hints, ClassLoader JavaDoc classloader) throws PException {
146         for(int i=0; i<namingManagers.length; i++) {
147             if (namingManagers[i].canProvidePBinder(hints, classloader)) {
148                 return namingManagers[i];
149             }
150         }
151         throw new PException("No identity manager found for the hints '"
152                 + hints + "'.");
153     }
154
155     public PName decode(PNameCoder pnc, Object JavaDoc oid, Class JavaDoc clazz, JormFactory jf) throws PException {
156         for(int i=0; i<namingManagers.length; i++) {
157             PName pn = namingManagers[i].decode(pnc, oid, clazz, jf);
158             if (pn != null) {
159                 return pn;
160             }
161         }
162         throw new PException("No identity manager able to decode:"
163                 + "\n\tpnc=" + pnc
164                 + "\n\toid=" + oid
165                 + "\n\tclazz=" + clazz);
166     }
167
168     public Object JavaDoc encode(PName pn) throws PException {
169         for(int i=0; i<namingManagers.length; i++) {
170             Object JavaDoc o = namingManagers[i].encode(pn);
171             if (o != null) {
172                 return o;
173             }
174         }
175         throw new PException("No identity manager able to encode ("
176                 + pn + ")");
177     }
178 }
179
Popular Tags