1 23 24 package org.objectweb.jorm.compiler.lib; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 28 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 29 import org.objectweb.jorm.generator.api.Generator; 30 import org.objectweb.jorm.generator.api.MOPFactory; 31 import org.objectweb.jorm.lib.JormConfiguratorImpl; 32 import org.objectweb.jorm.util.api.Loggable; 33 import org.objectweb.jorm.verifier.api.MappingVerifier; 34 import org.objectweb.jorm.verifier.api.Verifier; 35 import org.objectweb.util.monolog.api.BasicLevel; 36 import org.objectweb.util.monolog.api.LoggerFactory; 37 38 import java.io.File ; 39 import java.io.FileInputStream ; 40 import java.io.FileNotFoundException ; 41 import java.io.InputStream ; 42 import java.util.HashMap ; 43 44 49 public class JormCompilerConfiguratorImpl extends JormConfiguratorImpl implements JormCompilerConfigurator { 50 51 private HashMap mapperName2mofFactory = new HashMap (); 52 53 57 public void setLoggerFactory(LoggerFactory lf) { 58 loggerFactory = lf; 59 logger = lf.getLogger(JormCompilerConfigurator.LOGGER_NAME); 60 } 61 62 66 public Verifier getVerifier() throws PException { 67 if (properties == null) { 68 throw new PExceptionCompiler("JORM has not been configured."); 69 } 70 String cn = properties.getProperty("jorm.verifier"); 71 if (cn == null) { 72 throw new PException( 73 "Impossible to load a verifier: not configured:" 74 + properties); 75 } 76 try { 77 return (Verifier) loader.loadClass(cn).newInstance(); 78 } catch (InstantiationException e) { 79 throw new PExceptionCompiler(e, "Problem while instantiating " + cn); 80 } catch (IllegalAccessException e) { 81 throw new PExceptionCompiler(e, "Problem while trying to access " + cn); 82 } catch (ClassNotFoundException e) { 83 throw new PExceptionCompiler(e, "Problem while loading class " + cn); 84 } 85 } 86 87 91 public Generator getGenerator() throws PException { 92 if (properties == null) { 93 throw new PExceptionCompiler("JORM has not been configured."); 94 } 95 String cn = properties.getProperty("jorm.generator"); 96 if (cn == null) { 97 throw new PException( 98 "Impossible to load a generator: not configured:" 99 + properties); 100 } 101 try { 102 Generator res = (Generator) loader.loadClass(cn).newInstance(); 103 return res; 104 } catch (InstantiationException e) { 105 throw new PExceptionCompiler(e, "Problem while instantiating " + cn); 106 } catch (IllegalAccessException e) { 107 throw new PExceptionCompiler(e, "Problem while trying to access " + cn); 108 } catch (ClassNotFoundException e) { 109 throw new PExceptionCompiler(e, "Problem while loading class " + cn); 110 } 111 } 112 113 117 public String getJormcOptsFile() throws PException { 118 if (properties == null) { 119 throw new PExceptionCompiler("JORM has not been configured."); 120 } 121 return properties.getProperty("jorm.compiler.optsfile"); 122 } 123 124 130 public MOPFactory getMOPFactory(String mappername) throws PException { 131 MOPFactory mopFactory = (MOPFactory) mapperName2mofFactory.get(mappername); 132 if (mopFactory != null) { 133 return mopFactory; 134 } 135 if (properties == null) { 136 throw new PExceptionCompiler("JORM has not been configured."); 137 } 138 String cn = properties.getProperty("jorm.mapper.mopfactory." + mappername); 139 if (cn == null) { 140 throw new PException( 141 "Impossible to load a meta object factory: not configured:" 142 + properties); 143 } 144 try { 145 mopFactory = (MOPFactory) loader.loadClass(cn).newInstance(); 146 mapperName2mofFactory.put(mappername, mopFactory); 147 if (mopFactory instanceof Loggable) { 148 if (loggerFactory != null) { 149 ((Loggable) mopFactory).setLoggerFactory(loggerFactory); 150 } else if (logger != null) { 151 ((Loggable) mopFactory).setLogger(logger); 152 } 153 } 154 return mopFactory; 155 } catch (InstantiationException e) { 156 throw new PExceptionCompiler(e, "Problem while instantiating " + cn); 157 } catch (IllegalAccessException e) { 158 throw new PExceptionCompiler(e, "Problem while trying to access " + cn); 159 } catch (ClassNotFoundException e) { 160 throw new PExceptionCompiler(e, "Problem while loading class " + cn); 161 } 162 } 163 164 169 public InputStream getGlobalJormcOptsFile() throws PException { 170 InputStream res = null; 171 String jormcOptsFile = getJormcOptsFile(); 172 if (pathToJormcOpts != null) { 173 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 174 logger.log(BasicLevel.DEBUG, "JormcOptsFile=" + pathToJormcOpts 175 + File.separator + jormcOptsFile); 176 } 177 File f = new File ( 178 pathToJormcOpts + File.separator + jormcOptsFile); 179 if (f.exists()) { 180 try { 181 res = new FileInputStream (f); 182 } catch (FileNotFoundException e) { 183 throw new PException(e, "Cannot find jorm compiler option file."); 184 } 185 } 186 } 187 if (res == null) { 188 File f = new File (jormcOptsFile); 189 if (f.exists()) { 190 try { 191 res = new FileInputStream (f); 192 } catch (FileNotFoundException e) { 193 throw new PException(e, "Cannot find jorm compiler option file."); 194 } 195 } else { 196 res = loader.getResourceAsStream(jormcOptsFile); 197 } 198 if (res == null) { 199 throw new PExceptionCompiler("Problem while accessing global " + 200 "compiler parameters: unable to locate [" 201 + getJormcOptsFile() + "]"); 202 } 203 } 204 return res; 205 } 206 207 213 public MappingVerifier getMappingVerifier(String mappername) throws PException { 214 if (properties == null) { 215 throw new PExceptionCompiler("JORM has not been configured."); 216 } 217 String cn = properties.getProperty("jorm.mapper.verifier." + mappername); 218 try { 219 MappingVerifier v = (MappingVerifier) loader.loadClass(cn).newInstance(); 220 return v; 221 } catch (InstantiationException e) { 222 throw new PExceptionCompiler(e, "Problem while instantiating " + cn); 223 } catch (IllegalAccessException e) { 224 throw new PExceptionCompiler(e, "Problem while trying to access " + cn); 225 } catch (ClassNotFoundException e) { 226 throw new PExceptionCompiler(e, "Problem while loading class " + cn); 227 } 228 } 229 } 230 | Popular Tags |