1 45 package org.openejb.webadmin.main; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.FileOutputStream ; 50 import java.io.IOException ; 51 import java.io.ObjectInputStream ; 52 import java.io.ObjectOutputStream ; 53 import java.io.PrintWriter ; 54 import java.util.Properties ; 55 56 import javax.ejb.Handle ; 57 import javax.naming.Context ; 58 import javax.naming.InitialContext ; 59 import javax.rmi.PortableRemoteObject ; 60 61 import org.exolab.castor.jdo.conf.Database; 62 import org.exolab.castor.jdo.conf.DatabaseChoice; 63 import org.exolab.castor.jdo.conf.Driver; 64 import org.exolab.castor.jdo.conf.Jndi; 65 import org.exolab.castor.jdo.conf.Mapping; 66 import org.exolab.castor.jdo.conf.Param; 67 import org.exolab.castor.xml.ValidationException; 68 import org.openejb.core.EnvProps; 69 import org.openejb.loader.SystemInstance; 70 import org.openejb.util.FileUtils; 71 import org.openejb.webadmin.HttpRequest; 72 import org.openejb.webadmin.HttpResponse; 73 import org.openejb.webadmin.WebAdminBean; 74 75 78 public class CMPMappingBean extends WebAdminBean { 79 80 private static final String HANDLE_FILE = System.getProperty("file.separator") + "configurationHandle.obj"; 81 82 83 public void ejbCreate() { 84 this.section = "CMPMapping"; 85 } 86 87 92 public void preProcess(HttpRequest request, HttpResponse response) throws IOException {} 93 94 99 public void postProcess(HttpRequest request, HttpResponse response) throws IOException {} 100 101 109 public void writeHtmlTitle(PrintWriter body) throws IOException { 110 body.println(HTML_TITLE); 111 } 112 113 119 public void writePageTitle(PrintWriter body) throws IOException { 120 body.println("Container Managed Persistance Mapping"); 121 } 122 123 143 public void writeSubMenuItems(PrintWriter body) throws IOException {} 144 145 152 public void writeBody(PrintWriter body) throws IOException { 153 172 body.println("Coming soon..."); 173 } 174 175 178 private boolean submitDatabaseInformation(PrintWriter body, CMPMappingDataObject dataObject, String handleFile) throws IOException { 179 183 DatabaseData databaseData = new DatabaseData(); 184 databaseData.setDbEngine(request.getFormParameter(CMPMappingWriter.FORM_FIELD_DB_ENGINE)); 185 databaseData.setDriverClass(request.getFormParameter(CMPMappingWriter.FORM_FIELD_DRIVER_CLASS)); 186 databaseData.setDriverUrl(request.getFormParameter(CMPMappingWriter.FORM_FIELD_DRIVER_URL)); 187 databaseData.setFileName(request.getFormParameter(CMPMappingWriter.FORM_FIELD_FILE_NAME)); 188 databaseData.setJndiName(request.getFormParameter(CMPMappingWriter.FORM_FIELD_JNDI_NAME)); 189 databaseData.setPassword(request.getFormParameter(CMPMappingWriter.FORM_FIELD_PASSWORD)); 190 databaseData.setUsername(request.getFormParameter(CMPMappingWriter.FORM_FIELD_USERNAME)); 191 192 try { 194 databaseData.validate(); 195 } catch (ValidationException e) { 196 CMPMappingWriter.printDBInfo(body, e.getMessage(), databaseData, handleFile); 197 return false; 198 } 199 200 String path = 202 SystemInstance.get().getBase().getDirectory("conf").getAbsolutePath() 203 + System.getProperty("file.separator") 204 + databaseData.getFileName(); 205 206 String localDBFileName = path + ".cmp_local_database.xml"; 208 String globalDBFileName = path + ".cmp_global_database.xml"; 209 String mappingFileName = path + ".cmp_or_mapping.xml"; 210 211 Database globalDatabase = new Database(); 213 DatabaseChoice globalDatabaseChoice = new DatabaseChoice(); 214 Database localDatabase = new Database(); 215 DatabaseChoice localDatabaseChoice = new DatabaseChoice(); 216 globalDatabase.setName(EnvProps.GLOBAL_TX_DATABASE); 217 globalDatabase.setEngine(databaseData.getDbEngine()); 218 localDatabase.setName(EnvProps.LOCAL_TX_DATABASE); 219 localDatabase.setEngine(databaseData.getDbEngine()); 220 221 Mapping mapping = new Mapping(); 223 mapping.setHref(mappingFileName); 224 globalDatabase.addMapping(mapping); 225 localDatabase.addMapping(mapping); 226 227 Jndi jndi = new Jndi(); 229 jndi.setName(databaseData.getJndiName()); 230 globalDatabaseChoice.setJndi(jndi); 231 232 Driver driver = new Driver(); 234 Param userNameParam = new Param(); 235 Param passwordParam = new Param(); 236 237 userNameParam.setName("user"); 239 userNameParam.setValue(databaseData.getUsername()); 240 passwordParam.setName("password"); 241 passwordParam.setValue(databaseData.getPassword()); 242 243 driver.setClassName(databaseData.getDriverClass()); 245 driver.setUrl(databaseData.getDriverUrl()); 246 driver.addParam(userNameParam); 247 driver.addParam(passwordParam); 248 249 localDatabaseChoice.setDriver(driver); 250 251 localDatabase.setDatabaseChoice(localDatabaseChoice); 252 globalDatabase.setDatabaseChoice(globalDatabaseChoice); 253 254 try { 256 localDatabase.validate(); 257 globalDatabase.validate(); 258 } catch (ValidationException e) { 259 CMPMappingWriter.printDBInfo(body, e.getMessage(), databaseData, handleFile); 260 return false; 261 } 262 263 File jdbcDriverSource = new File (request.getFormParameter(CMPMappingWriter.FORM_FIELD_JDBC_DRIVER)); 265 String libDir = 266 SystemInstance.get().getBase().getDirectory("lib").getAbsolutePath() 267 + System.getProperty("file.separator") 268 + jdbcDriverSource.getName(); 269 File destFile = new File (libDir); 270 271 if (jdbcDriverSource.isFile()) { 273 if (!destFile.exists() && !destFile.createNewFile()) { 274 throw new IOException ("Could not create file: " + libDir); 275 } 276 277 FileUtils.copyFile(destFile, jdbcDriverSource); 278 } 279 280 dataObject.setGlobalDatabase(globalDatabase); 282 dataObject.setGlobalDatabaseFileName(globalDBFileName); 283 dataObject.setLocalDatabase(localDatabase); 284 dataObject.setLocalDatabaseFileName(localDBFileName); 285 dataObject.setMappingRootFileName(mappingFileName); 286 287 291 return true; 292 } 293 294 301 private String createHandle(CMPMappingDataObject mappingData) throws IOException { 302 File myHandleFile = new File (FileUtils.createTempDirectory().getAbsolutePath() + HANDLE_FILE); 304 if (!myHandleFile.exists()) { 305 myHandleFile.createNewFile(); 306 } 307 308 ObjectOutputStream objectOut = new ObjectOutputStream (new FileOutputStream (myHandleFile)); 309 objectOut.writeObject(mappingData.getHandle()); objectOut.flush(); 311 objectOut.close(); 312 313 return myHandleFile.getAbsolutePath(); 314 } 315 316 322 private CMPMappingDataObject getCMPMappingDataObject() throws IOException { 323 Properties p = new Properties (); 324 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.core.ivm.naming.InitContextFactory"); 325 326 try { 328 InitialContext ctx = new InitialContext (p); 329 Object obj = ctx.lookup("mapping/webadmin/CMPMappingData"); 330 CMPMappingDataHome home = (CMPMappingDataHome) PortableRemoteObject.narrow(obj, CMPMappingDataHome.class); 332 return home.create(); 333 } catch (Exception e) { 334 throw new IOException (e.getMessage()); 335 } 336 } 337 338 345 private CMPMappingDataObject getHandle(String handleFile) throws IOException { 346 File myHandleFile = new File (handleFile); 347 348 ObjectInputStream objectIn = new ObjectInputStream (new FileInputStream (myHandleFile)); 350 Handle mappingHandle; 352 try { 353 mappingHandle = (Handle ) objectIn.readObject(); 354 return (CMPMappingDataObject) mappingHandle.getEJBObject(); 355 } catch (Exception e) { 356 throw new IOException (e.getMessage()); 357 } 358 } 359 } | Popular Tags |