KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > ejb > EJBControlAssembler


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.lang.String JavaDoc;
34
35 /**
36  * The EJBControl needs to inject EJB reference entries into the
37  * DD of its containing module for cases where ejb-link is used.
38  */

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 JavaDoc 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 JavaDoc ejbLinkValue = ea.ejbLink();
57         if ( ejbLinkValue == null || ejbLinkValue.length() == 0 )
58         {
59             // Not using ejb-link, so no ejb-ref injection needed
60
return;
61         }
62
63         if (cac instanceof ControlAssemblyContext.EJBModule)
64         {
65             // insert any required <ejb-ref> entries into the deployment descriptor
66
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 JavaDoc 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 JavaDoc ejbJarFile = ejbCtx.getEjbJarXml();
87         FileInputStream JavaDoc ejbJarStream = null;
88         try
89         {
90             ejbJarStream = new FileInputStream JavaDoc( ejbJarFile );
91         }
92         catch (FileNotFoundException JavaDoc 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             // get name of EJB reference to be inserted
104
String JavaDoc insertedEjbRefName = ei._refName;
105
106             // get the existing <ejb-jar> XBean from the stream
107
EjbJarDocument ejbDoc = EjbJarDocument.Factory.parse(ejbJarStream);
108
109             // copy ejbDoc XmlObject so can makes changes to it
110
ejbDoc = (EjbJarDocument)ejbDoc.copy();
111             EjbJarType ejbJarType = ejbDoc.getEjbJar();
112             EnterpriseBeansType entBeansType = ejbJarType.getEnterpriseBeans();
113
114             // TODO: support Entity & other bean types
115
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             // overwrite existing ejb-jar.xml file with new document
128
XmlOptions xmlOpts = new XmlOptions();
129             xmlOpts.setSavePrettyPrint();
130             ejbDoc.save(ejbJarFile, xmlOpts);
131         }
132         catch(IOException JavaDoc 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 JavaDoc e)
154             {
155                 // do nothing
156
}
157         }
158     }
159
160     protected void insertEJBRefInEJBJar(SessionBeanType sessionBean, EJBInfo ei, String JavaDoc ejbLinkValue)
161     {
162         System.err.println("EJBControlAssembler.insertEJBRefInEJBJar() called");
163         System.err.println("ejbLinkValue =" + ejbLinkValue );
164         EjbRefType[] ejbRefArray = sessionBean.getEjbRefArray();
165         String JavaDoc 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 JavaDoc ejbRefName = ejbRef.getEjbRefName().getStringValue();
174                 if (insertedEjbRefName.equals(ejbRefName))
175                 {
176                     sessionBean.removeEjbRef(j);
177                     break;
178                 }
179             }
180             // insert a new <ejb-ref> entry and fill in the values
181
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 JavaDoc ejbLinkValue)
208     {
209         System.err.println("EJBControlAssembler.insertEJBLocalRefInEJBJar() called");
210         System.err.println("ejbLinkValue =" + ejbLinkValue );
211         EjbLocalRefType[] ejbLocalRefArray = sessionBean.getEjbLocalRefArray();
212         String JavaDoc 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 JavaDoc ejbRefName = ejbLocalRef.getEjbRefName().getStringValue();
221                 if (insertedEjbRefName.equals(ejbRefName))
222                 {
223                     sessionBean.removeEjbLocalRef(j);
224                     break;
225                 }
226             }
227             // insert a new <ejb-local-ref> entry and fill in the values
228
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 JavaDoc 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 JavaDoc webXmlFile = webAppCcc.getWebXml();
262             FileInputStream JavaDoc webXmlStream = null;
263             try
264             {
265                 webXmlStream = new FileInputStream JavaDoc( webXmlFile );
266             }
267             catch (FileNotFoundException JavaDoc 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                 // get the existing <web-app> XBean from the stream
279
WebAppDocument webAppDoc = WebAppDocument.Factory.parse(webXmlStream);
280
281                 // copy webAppDoc XmlObject so can makes changes to it
282
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                 // overwrite existing web.xml file with new document
291
XmlOptions xmlOpts = new XmlOptions();
292                 xmlOpts.setSavePrettyPrint();
293                 webAppDoc.save(webXmlFile, xmlOpts);
294             }
295             catch(IOException JavaDoc 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 JavaDoc e)
317                 {
318                     // do nothing
319
}
320             }
321
322         }
323
324     protected void insertEJBRefInWebApp(WebAppType webAppType, EJBInfo ei, String JavaDoc ejbLinkValue)
325     {
326         System.err.println("EJBControlAssembler.insertEJBRefInWebApp() called");
327         System.err.println("ejbLinkValue =" + ejbLinkValue );
328
329         EjbRefType[] ejbRefArray = webAppType.getEjbRefArray();
330         String JavaDoc 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 JavaDoc ejbRefName = ejbRef.getEjbRefName().getStringValue();
339                 if (insertedEjbRefName.equals(ejbRefName))
340                 {
341                     webAppType.removeEjbRef(j);
342                     break;
343                 }
344             }
345             // insert a new <ejb-ref> entry and fill in the values
346
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 JavaDoc ejbLinkValue)
373     {
374         System.err.println("EJBControlAssembler.insertEJBLocalRefInWebApp() called");
375         System.err.println("ejbLinkValue =" + ejbLinkValue );
376
377         EjbLocalRefType[] ejbLocalRefArray = webAppType.getEjbLocalRefArray();
378         String JavaDoc 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 JavaDoc ejbRefName = ejbLocalRef.getEjbRefName().getStringValue();
387                 if (insertedEjbRefName.equals(ejbRefName))
388                 {
389                     webAppType.removeEjbLocalRef(j);
390                     break;
391                 }
392             }
393             // insert a new <ejb-local-ref> entry and fill in the values
394
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