1 23 24 package org.objectweb.jorm.lib; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.api.PMappingStructuresManager; 28 import org.objectweb.jorm.api.JormConfigurator; 29 import org.objectweb.jorm.metainfo.api.Manager; 30 import org.objectweb.jorm.metainfo.api.MappingFactory; 31 import org.objectweb.jorm.xml2mi.api.MappingParser; 32 import org.objectweb.jorm.xml2mi.api.Parser; 33 import org.objectweb.jorm.mi2xml.api.MappingDomtreeBuilder; 34 import org.objectweb.jorm.mi2xml.api.Writer; 35 import org.objectweb.jorm.util.api.Loggable; 36 import org.objectweb.util.monolog.Monolog; 37 import org.objectweb.util.monolog.api.Logger; 38 import org.objectweb.util.monolog.api.LoggerFactory; 39 import org.objectweb.util.monolog.api.MonologFactory; 40 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.util.ArrayList ; 46 import java.util.Iterator ; 47 import java.util.Properties ; 48 import java.util.StringTokenizer ; 49 import java.util.Collection ; 50 import java.util.HashMap ; 51 52 56 public class JormConfiguratorImpl implements JormConfigurator { 57 protected Properties properties = null; 58 protected Properties logProperties = null; 59 protected String pathToJormcOpts = null; 60 protected HashMap mappers = new HashMap (); 61 protected LoggerFactory loggerFactory = null; 62 protected Logger logger = null; 63 protected ClassLoader loader = null; 64 65 70 public void configure(String propertyfile) throws PException { 71 if (properties != null) { 72 throw new PException("JORM already configured."); 73 } 74 try { 75 File pf = new File (propertyfile); 76 properties = new Properties (); 77 properties.load(new FileInputStream (pf)); 78 configure(properties); 79 pathToJormcOpts = pf.getParent(); 80 } catch (IOException e) { 81 InputStream is = getClass().getClassLoader().getResourceAsStream(propertyfile); 82 if (is == null) { 83 throw new PException(e, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]"); 84 } 85 try { 86 properties = new Properties (); 87 properties.load(is); 88 configure(properties); 89 } catch (Exception e2) { 90 properties = null; 91 throw new PException(e2, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]"); 92 } 93 } 94 } 95 96 public void configure() throws PException { 97 configure(DEFAULT_JORM_CONFIGURATION_FILE); 98 } 99 100 105 public void configure(Properties p) { 106 properties = p; 107 StringTokenizer st = new StringTokenizer (p.getProperty("jorm.mapper.list").trim(), ","); 108 while (st.hasMoreTokens()) { 109 String mappername = st.nextToken(); 110 String sms = properties.getProperty("jorm.mapper.submappers." + mappername); 111 if (sms == null) { 112 continue; 113 } 114 ArrayList sml = new ArrayList (); 115 mappers.put(mappername, sml); 116 StringTokenizer st2 = new StringTokenizer (sms.trim(), ",", false); 117 while (st2.hasMoreTokens()) { 118 sml.add(st2.nextToken()); 119 } 120 } 121 122 String useCtxLoader = properties.getProperty(USE_CONTEXT_CLASSLOADER, "false").trim(); 124 if ("false".equals(useCtxLoader)) { 125 loader = getClass().getClassLoader(); 126 } else { 127 loader = Thread.currentThread().getContextClassLoader(); 128 } 129 } 130 131 135 public void configureLog(String propertyfile) throws PException { 136 if (loggerFactory == null) { 137 loggerFactory = Monolog.getMonologFactory(propertyfile); 138 } else { 139 Properties p = new Properties (); 140 try { 141 File pf = new File (propertyfile); 142 p.load(new FileInputStream (pf)); 143 } catch (IOException e) { 144 try { 145 p.load(loader.getResourceAsStream(propertyfile)); 146 } catch (Exception e2) { 147 throw new PException(e, "Problem while configuring the JORM: unable to locate [" + propertyfile + "]"); 148 } 149 } 150 Monolog.loadMonologConfiguration(p, (MonologFactory) loggerFactory); 151 } 152 } 153 154 158 public Iterator knownMappers() throws PException { 159 if (properties == null) { 160 throw new PException("JORM has not been configured."); 161 } 162 return mappers.keySet().iterator(); 163 } 164 165 169 public void setLoggerFactory(LoggerFactory lf) { 170 loggerFactory = lf; 171 logger = lf.getLogger(LOGGER_NAME); 172 } 173 174 178 public LoggerFactory getLoggerFactory() throws PException { 179 if (loggerFactory == null) { 180 throw new PException("JORM has not been configured."); 181 } 182 return loggerFactory; 183 } 184 185 189 public Manager getMIManager() throws PException { 190 if (properties == null) { 191 throw new PException("JORM has not been configured."); 192 } 193 String cn = properties.getProperty("jorm.mimanager"); 194 if (cn == null) { 195 throw new PException( 196 "Impossible to load a meta info manager: not configured:" 197 + properties); 198 } 199 try { 200 Manager m = (Manager) loader.loadClass(cn).newInstance(); 201 if (loggerFactory != null && m instanceof Loggable) { 202 ((Loggable) m).setLoggerFactory(loggerFactory); 203 } 204 return m; 205 } catch (InstantiationException e) { 206 throw new PException(e, "Problem while instantiating " + cn); 207 } catch (IllegalAccessException e) { 208 throw new PException(e, "Problem while trying to access " + cn); 209 } catch (ClassNotFoundException e) { 210 throw new PException(e, "Problem while loading class " + cn); 211 } 212 } 213 214 218 public Parser getParser() throws PException { 219 if (properties == null) { 220 throw new PException("JORM has not been configured."); 221 } 222 String cn = properties.getProperty("jorm.parser"); 223 if (cn == null) { 224 throw new PException( 225 "Impossible to load a parser: not configured:" 226 + properties); 227 } 228 try { 229 return (Parser) loader.loadClass(cn).newInstance(); 230 } catch (InstantiationException e) { 231 throw new PException(e, "Problem while instantiating " + cn); 232 } catch (IllegalAccessException e) { 233 throw new PException(e, "Problem while trying to access " + cn); 234 } catch (ClassNotFoundException e) { 235 throw new PException(e, "Problem while loading class " + cn); 236 } 237 } 238 239 245 public MappingFactory getMIFactory(String mappername) throws PException { 246 if (properties == null) { 247 throw new PException("JORM has not been configured."); 248 } 249 String cn = properties.getProperty("jorm.mapper.mifactory." + mappername); 250 if (cn == null) { 251 throw new PException( 252 "Impossible to load a meta info factory: not configured:" 253 + "jorm.mapper.mifactory." + mappername); 254 } 255 try { 256 MappingFactory mf = (MappingFactory) loader.loadClass(cn).newInstance(); 257 return mf; 258 } catch (InstantiationException e) { 259 throw new PException(e, "Problem while instantiating " + cn); 260 } catch (IllegalAccessException e) { 261 throw new PException(e, "Problem while trying to access " + cn); 262 } catch (ClassNotFoundException e) { 263 throw new PException(e, "Problem while loading class " + cn); 264 } 265 } 266 267 273 public PMappingStructuresManager getSchMgr(String mappername) throws PException { 274 if (properties == null) { 275 throw new PException("JORM has not been configured."); 276 } 277 String cn = properties.getProperty("jorm.mapper.schmgr." + mappername); 278 if (cn == null) { 279 throw new PException( 280 "Impossible to load a schema manager: not configured:" 281 + properties); 282 } 283 try { 284 PMappingStructuresManager mf = (PMappingStructuresManager) loader.loadClass(cn).newInstance(); 285 return mf; 286 } catch (InstantiationException e) { 287 throw new PException(e, "Problem while instantiating " + cn); 288 } catch (IllegalAccessException e) { 289 throw new PException(e, "Problem while trying to access " + cn); 290 } catch (ClassNotFoundException e) { 291 throw new PException(e, "Problem while loading class " + cn); 292 } 293 } 294 295 301 public Class getGcmClass(String mappername) throws PException { 302 if (properties == null) { 303 throw new PException("JORM has not been configured."); 304 } 305 String cn = properties.getProperty("jorm.mapper.gcmapping." + mappername); 306 if (cn == null) { 307 throw new PException( 308 "Impossible to load a generic class mapping: not configured:" 309 + properties); 310 } 311 try { 312 Class res = loader.loadClass(cn); 313 res.newInstance(); 314 return res; 315 } catch (InstantiationException e) { 316 throw new PException(e, "Problem while trying to instantiate " + cn); 317 } catch (IllegalAccessException e) { 318 throw new PException(e, "Problem while trying to access " + cn); 319 } catch (ClassNotFoundException e) { 320 throw new PException(e, "Problem while loading class " + cn); 321 } 322 } 323 324 330 public MappingParser getMappingParser(String mappername) throws PException { 331 if (properties == null) { 332 throw new PException("JORM has not been configured."); 333 } 334 String cn = properties.getProperty("jorm.mapper.parser." + mappername); 335 try { 336 MappingParser p = (MappingParser) loader.loadClass(cn).newInstance(); 337 return p; 338 } catch (InstantiationException e) { 339 throw new PException(e, "Problem while instantiating " + cn); 340 } catch (IllegalAccessException e) { 341 throw new PException(e, "Problem while trying to access " + cn); 342 } catch (ClassNotFoundException e) { 343 throw new PException(e, "Problem while loading class " + cn); 344 } 345 } 346 347 352 public Writer getWriter() throws PException { 353 if (properties == null) { 354 throw new PException("JORM has not been configured."); 355 } 356 String cn = properties.getProperty("jorm.writer"); 357 try { 358 return (Writer) loader.loadClass(cn).newInstance(); 359 } catch (InstantiationException e) { 360 throw new PException(e, "Problem while instantiating " + cn); 361 } catch (IllegalAccessException e) { 362 throw new PException(e, "Problem while trying to access " + cn); 363 } catch (ClassNotFoundException e) { 364 throw new PException(e, "Problem while loading class " + cn); 365 } 366 } 367 368 374 public MappingDomtreeBuilder getMappingDomtreeBuilder(String mappername) 375 throws PException { 376 if (properties == null) { 377 throw new PException("JORM has not been configured."); 378 } 379 String cn = properties.getProperty("jorm.mapper.writer." + mappername); 380 try { 381 MappingDomtreeBuilder mdg = (MappingDomtreeBuilder) loader.loadClass(cn).newInstance(); 382 return mdg; 383 } catch (InstantiationException e) { 384 throw new PException(e, "Problem while instantiating " + cn); 385 } catch (IllegalAccessException e) { 386 throw new PException(e, "Problem while trying to access " + cn); 387 } catch (ClassNotFoundException e) { 388 throw new PException(e, "Problem while loading class " + cn); 389 } 390 } 391 392 398 public Collection getSubMappers(String mappername) throws PException { 399 if (properties == null) { 400 throw new PException("JORM has not been configured."); 401 } 402 return (Collection ) mappers.get(mappername); 403 } 404 405 409 public void removeMapper(String mn) { 410 mappers.remove(mn); 411 } 412 413 416 public void removeAllMappers() { 417 mappers.clear(); 418 } 419 420 425 public void removeSubMapper(String mn, String smn) { 426 ArrayList sms = (ArrayList ) mappers.get(mn); 427 if (sms == null) { 428 return; 429 } 430 int pos = sms.indexOf(smn); 431 if (pos == -1) { 432 return; 433 } 434 sms.remove(pos); 435 } 436 437 442 public void addSubMapper(String mn, String smn) { 443 ArrayList sms = (ArrayList ) mappers.get(mn); 444 if (sms == null) { 445 sms = new ArrayList (); 446 mappers.put(mn, sms); 447 } 448 int pos = sms.indexOf(smn); 449 if (pos == -1) { 450 sms.add(smn); 451 } 452 } 453 } 454 | Popular Tags |