1 18 package org.apache.beehive.controls.system.ejb; 19 20 import org.apache.beehive.controls.api.assembly.ControlAssembler; 21 import org.apache.beehive.controls.api.assembly.ControlAssemblyException; 22 import org.apache.beehive.controls.api.assembly.ControlAssemblyContext; 23 import org.apache.xmlbeans.XmlCursor; 24 import org.apache.xmlbeans.XmlException; 25 import org.apache.xmlbeans.XmlOptions; 26 27 import org.apache.beehive.controls.system.ejb.schemas.j2ee.*; 28 29 import java.io.IOException ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.lang.String ; 34 35 39 public class EJBControlAssembler implements ControlAssembler 40 { 41 public void assemble(ControlAssemblyContext cac) 42 throws ControlAssemblyException 43 { 44 System.err.println("EJBControlAssembler.assemble() called"); 45 46 Class controlInterface = cac.getControlType(); 47 EJBInfo ei = new EJBInfo( controlInterface ); 48 49 EJBControl.EJBHome ea = cac.getControlAnnotation(EJBControl.EJBHome.class); 50 if ( ea == null ) 51 { 52 System.err.println( "Missing EJBHome annotation on control?!"); 53 return; 54 } 55 56 String ejbLinkValue = ea.ejbLink(); 57 if ( ejbLinkValue == null || ejbLinkValue.length() == 0 ) 58 { 59 return; 61 } 62 63 if (cac instanceof ControlAssemblyContext.EJBModule) 64 { 65 updateEJBJar( (ControlAssemblyContext.EJBModule)cac, ei, ejbLinkValue ); 67 } 68 else if (cac instanceof ControlAssemblyContext.WebAppModule) 69 { 70 updateWebApp( (ControlAssemblyContext.WebAppModule)cac, ei, ejbLinkValue ); 71 } 72 else 73 { 74 System.err.println("EJBControlAssembler - no work to do, assembly context is not EJB."); 75 } 76 } 77 78 protected void updateEJBJar( ControlAssemblyContext.EJBModule ejbCtx, 79 EJBInfo ei, String ejbLinkValue ) 80 throws ControlAssemblyException 81 { 82 System.err.println("EJBControlAssembler.updateEJBJar() called"); 83 System.err.println(" ei=" + ei ); 84 System.err.println(" ejbLinkValue=" + ejbLinkValue ); 85 86 File ejbJarFile = ejbCtx.getEjbJarXml(); 87 FileInputStream ejbJarStream = null; 88 try 89 { 90 ejbJarStream = new FileInputStream ( ejbJarFile ); 91 } 92 catch (FileNotFoundException fnfe) 93 { 94 System.err.println("*** Warning *** EJBControlAssembler aborted: " + 95 "caught FileNotFoundException attempting to read file " + 96 ejbJarFile.getAbsolutePath() + ". Message: " + 97 fnfe.getMessage()); 98 return; 99 } 100 101 try 102 { 103 String insertedEjbRefName = ei._refName; 105 106 EjbJarDocument ejbDoc = EjbJarDocument.Factory.parse(ejbJarStream); 108 109 ejbDoc = (EjbJarDocument)ejbDoc.copy(); 111 EjbJarType ejbJarType = ejbDoc.getEjbJar(); 112 EnterpriseBeansType entBeansType = ejbJarType.getEnterpriseBeans(); 113 114 SessionBeanType[] sessionArray = entBeansType.getSessionArray(); 116 for (int i=0; i<sessionArray.length; i++) 117 { 118 SessionBeanType sessionBean = sessionArray[i]; 119 120 if (ei._isLocal) 121 insertEJBLocalRefInEJBJar(sessionBean, ei, ejbLinkValue); 122 else 123 insertEJBRefInEJBJar(sessionBean, ei, ejbLinkValue); 124 125 } 126 127 XmlOptions xmlOpts = new XmlOptions(); 129 xmlOpts.setSavePrettyPrint(); 130 ejbDoc.save(ejbJarFile, xmlOpts); 131 } 132 catch(IOException ioe) 133 { 134 System.err.println("EJBControlAssembler: caught IOException " + 135 "attempting to write to file " + ejbJarFile.getAbsolutePath() + 136 ". Message: " + ioe.getMessage()); 137 } 138 catch(XmlException xe) 139 { 140 System.err.println("EJBControlAssembler: caught XmlException " + 141 "attempting xml manipulations. Message: " + 142 xe.getMessage()); 143 } 144 finally 145 { 146 try 147 { 148 if (ejbJarStream != null) 149 { 150 ejbJarStream.close(); 151 } 152 } 153 catch(IOException e) 154 { 155 } 157 } 158 } 159 160 protected void insertEJBRefInEJBJar(SessionBeanType sessionBean, EJBInfo ei, String ejbLinkValue) 161 { 162 System.err.println("EJBControlAssembler.insertEJBRefInEJBJar() called"); 163 System.err.println("ejbLinkValue =" + ejbLinkValue ); 164 EjbRefType[] ejbRefArray = sessionBean.getEjbRefArray(); 165 String insertedEjbRefName = ei._refName; 166 XmlCursor ejbJarXmlCursor = null; 167 168 try { 169 for (int j=ejbRefArray.length-1; j>=0; j--) 170 { 171 EjbRefType ejbRef = ejbRefArray[j]; 172 ejbJarXmlCursor = ejbRef.newCursor(); 173 String ejbRefName = ejbRef.getEjbRefName().getStringValue(); 174 if (insertedEjbRefName.equals(ejbRefName)) 175 { 176 sessionBean.removeEjbRef(j); 177 break; 178 } 179 } 180 EjbRefType insertedEjbRef = sessionBean.insertNewEjbRef(0); 182 183 EjbRefNameType ejbRefName = insertedEjbRef.addNewEjbRefName(); 184 ejbRefName.setStringValue(insertedEjbRefName); 185 186 EjbRefTypeType ejbRefType = insertedEjbRef.addNewEjbRefType(); 187 ejbRefType.setStringValue(ei._beanType); 188 189 HomeType homeType = insertedEjbRef.addNewHome(); 190 homeType.setStringValue(ei._homeInterface.getName()); 191 RemoteType remoteType = insertedEjbRef.addNewRemote(); 192 remoteType.setStringValue(ei._beanInterface.getName()); 193 194 EjbLinkType ejbLink = insertedEjbRef.addNewEjbLink(); 195 ejbLink.setStringValue(ejbLinkValue); 196 } 197 finally 198 { 199 if (ejbJarXmlCursor != null) 200 { 201 ejbJarXmlCursor.dispose(); 202 } 203 } 204 205 } 206 207 protected void insertEJBLocalRefInEJBJar(SessionBeanType sessionBean, EJBInfo ei, String ejbLinkValue) 208 { 209 System.err.println("EJBControlAssembler.insertEJBLocalRefInEJBJar() called"); 210 System.err.println("ejbLinkValue =" + ejbLinkValue ); 211 EjbLocalRefType[] ejbLocalRefArray = sessionBean.getEjbLocalRefArray(); 212 String insertedEjbRefName = ei._refName; 213 XmlCursor ejbJarXmlCursor = null; 214 215 try { 216 for (int j=ejbLocalRefArray.length-1; j>=0; j--) 217 { 218 EjbLocalRefType ejbLocalRef = ejbLocalRefArray[j]; 219 ejbJarXmlCursor = ejbLocalRef.newCursor(); 220 String ejbRefName = ejbLocalRef.getEjbRefName().getStringValue(); 221 if (insertedEjbRefName.equals(ejbRefName)) 222 { 223 sessionBean.removeEjbLocalRef(j); 224 break; 225 } 226 } 227 EjbLocalRefType insertedEJBLocalRef = sessionBean.insertNewEjbLocalRef(0); 229 230 EjbRefNameType ejbRefName = insertedEJBLocalRef.addNewEjbRefName(); 231 ejbRefName.setStringValue(insertedEjbRefName); 232 233 EjbRefTypeType ejbRefType = insertedEJBLocalRef.addNewEjbRefType(); 234 ejbRefType.setStringValue(ei._beanType); 235 236 LocalHomeType homeType = insertedEJBLocalRef.addNewLocalHome(); 237 homeType.setStringValue(ei._homeInterface.getName()); 238 LocalType localType = insertedEJBLocalRef.addNewLocal(); 239 localType.setStringValue(ei._beanInterface.getName()); 240 241 EjbLinkType ejbLink = insertedEJBLocalRef.addNewEjbLink(); 242 ejbLink.setStringValue(ejbLinkValue); 243 } 244 finally 245 { 246 if (ejbJarXmlCursor != null) 247 { 248 ejbJarXmlCursor.dispose(); 249 } 250 } 251 252 } 253 254 protected void updateWebApp( ControlAssemblyContext.WebAppModule webAppCcc, 255 EJBInfo ei, String ejbLinkValue ) 256 throws ControlAssemblyException 257 { 258 System.err.println("EJBControlAssembler.updateWebApp() called"); 259 System.err.println("ei =" + ei); 260 System.err.println("ejbLinkValue =" + ejbLinkValue ); 261 File webXmlFile = webAppCcc.getWebXml(); 262 FileInputStream webXmlStream = null; 263 try 264 { 265 webXmlStream = new FileInputStream ( webXmlFile ); 266 } 267 catch (FileNotFoundException fnfe) 268 { 269 System.err.println("EJBControlAssembler: " + 270 "caught FileNotFoundException attempting to read file " + 271 webXmlFile.getAbsolutePath() + ". Message: " + 272 fnfe.getMessage()); 273 return; 274 } 275 276 try 277 { 278 WebAppDocument webAppDoc = WebAppDocument.Factory.parse(webXmlStream); 280 281 webAppDoc = (WebAppDocument)webAppDoc.copy(); 283 WebAppType webAppType = webAppDoc.getWebApp(); 284 285 if (ei._isLocal) 286 insertEJBLocalRefInWebApp(webAppType, ei, ejbLinkValue); 287 else 288 insertEJBRefInWebApp(webAppType, ei, ejbLinkValue); 289 290 XmlOptions xmlOpts = new XmlOptions(); 292 xmlOpts.setSavePrettyPrint(); 293 webAppDoc.save(webXmlFile, xmlOpts); 294 } 295 catch(IOException ioe) 296 { 297 System.err.println("ServiceControlAssembler: caught IOException " + 298 "attempting to write to file " + webXmlFile.getAbsolutePath() + 299 ". Message: " + ioe.getMessage()); 300 } 301 catch(XmlException xe) 302 { 303 System.err.println("ServiceControlAssembler: caught XmlException " + 304 "attempting xml manipulations. Message: " + 305 xe.getMessage()); 306 } 307 finally 308 { 309 try 310 { 311 if (webXmlStream != null) 312 { 313 webXmlStream.close(); 314 } 315 } 316 catch(IOException e) 317 { 318 } 320 } 321 322 } 323 324 protected void insertEJBRefInWebApp(WebAppType webAppType, EJBInfo ei, String ejbLinkValue) 325 { 326 System.err.println("EJBControlAssembler.insertEJBRefInWebApp() called"); 327 System.err.println("ejbLinkValue =" + ejbLinkValue ); 328 329 EjbRefType[] ejbRefArray = webAppType.getEjbRefArray(); 330 String insertedEjbRefName = ei._refName; 331 XmlCursor webXmlCursor = null; 332 333 try { 334 for (int j=ejbRefArray.length-1; j>=0; j--) 335 { 336 EjbRefType ejbRef = ejbRefArray[j]; 337 webXmlCursor = ejbRef.newCursor(); 338 String ejbRefName = ejbRef.getEjbRefName().getStringValue(); 339 if (insertedEjbRefName.equals(ejbRefName)) 340 { 341 webAppType.removeEjbRef(j); 342 break; 343 } 344 } 345 EjbRefType insertedEjbRef = webAppType.insertNewEjbRef(0); 347 348 EjbRefNameType ejbRefName = insertedEjbRef.addNewEjbRefName(); 349 ejbRefName.setStringValue(insertedEjbRefName); 350 351 EjbRefTypeType ejbRefType = insertedEjbRef.addNewEjbRefType(); 352 ejbRefType.setStringValue(ei._beanType); 353 354 HomeType homeType = insertedEjbRef.addNewHome(); 355 homeType.setStringValue(ei._homeInterface.getName()); 356 RemoteType remoteType = insertedEjbRef.addNewRemote(); 357 remoteType.setStringValue(ei._beanInterface.getName()); 358 359 EjbLinkType ejbLink = insertedEjbRef.addNewEjbLink(); 360 ejbLink.setStringValue(ejbLinkValue); 361 } 362 finally 363 { 364 if (webXmlCursor != null) 365 { 366 webXmlCursor.dispose(); 367 } 368 } 369 370 } 371 372 protected void insertEJBLocalRefInWebApp(WebAppType webAppType, EJBInfo ei, String ejbLinkValue) 373 { 374 System.err.println("EJBControlAssembler.insertEJBLocalRefInWebApp() called"); 375 System.err.println("ejbLinkValue =" + ejbLinkValue ); 376 377 EjbLocalRefType[] ejbLocalRefArray = webAppType.getEjbLocalRefArray(); 378 String insertedEjbRefName = ei._refName; 379 XmlCursor webXmlCursor = null; 380 381 try { 382 for (int j=ejbLocalRefArray.length-1; j>=0; j--) 383 { 384 EjbLocalRefType ejbLocalRef = ejbLocalRefArray[j]; 385 webXmlCursor = ejbLocalRef.newCursor(); 386 String ejbRefName = ejbLocalRef.getEjbRefName().getStringValue(); 387 if (insertedEjbRefName.equals(ejbRefName)) 388 { 389 webAppType.removeEjbLocalRef(j); 390 break; 391 } 392 } 393 EjbLocalRefType insertedEJBLocalRef = webAppType.insertNewEjbLocalRef(0); 395 396 EjbRefNameType ejbRefName = insertedEJBLocalRef.addNewEjbRefName(); 397 ejbRefName.setStringValue(insertedEjbRefName); 398 399 EjbRefTypeType ejbRefType = insertedEJBLocalRef.addNewEjbRefType(); 400 ejbRefType.setStringValue(ei._beanType); 401 402 LocalHomeType homeType = insertedEJBLocalRef.addNewLocalHome(); 403 homeType.setStringValue(ei._homeInterface.getName()); 404 LocalType localType = insertedEJBLocalRef.addNewLocal(); 405 localType.setStringValue(ei._beanInterface.getName()); 406 407 EjbLinkType ejbLink = insertedEJBLocalRef.addNewEjbLink(); 408 ejbLink.setStringValue(ejbLinkValue); 409 } 410 finally 411 { 412 if (webXmlCursor != null) 413 { 414 webXmlCursor.dispose(); 415 } 416 } 417 418 } 419 } 420 | Popular Tags |