KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > instance > DescriptorArchivist


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.instance;
25
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
32
33 import com.sun.enterprise.deployment.Application;
34 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
35 import com.sun.enterprise.deployment.archivist.Archivist;
36 import com.sun.enterprise.deployment.archivist.ArchivistFactory;
37 import com.sun.enterprise.deployment.BundleDescriptor;
38 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
39 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile;
40 import com.sun.enterprise.deployment.util.ModuleDescriptor;
41 import com.sun.enterprise.deployment.WebBundleDescriptor;
42 import com.sun.enterprise.deployment.WebServicesDescriptor;
43 import com.sun.enterprise.deployment.WebService;
44
45 /**
46  * This class is responsible for writing deployment descriptors
47  * after a deployment action has occured to a abstract archive instance.
48  *
49  * @author Jerome Dochez
50  */

51 public class DescriptorArchivist {
52     
53     /** Creates a new instance of DescriptorArchivist */
54     public DescriptorArchivist() {
55     }
56   
57     /**
58      * writes an application deployment descriptors
59      * @param the application object
60      * @param the abstract archive
61      */

62     public void write(Application application, AbstractArchive in,
63         AbstractArchive out) throws IOException JavaDoc {
64         if (application.isVirtual()) {
65             ModuleDescriptor aModule = (ModuleDescriptor) application.getModules().next();
66             Archivist moduleArchivist = ArchivistFactory.getArchivistForType(aModule.getModuleType());
67             write(aModule.getDescriptor(), moduleArchivist, in, out);
68         } else {
69             // this is a real application.
70

71             // let's start by writing out all submodules deployment descriptors
72
for (Iterator JavaDoc modules = application.getModules();modules.hasNext();) {
73                 ModuleDescriptor aModule = (ModuleDescriptor) modules.next();
74                 Archivist moduleArchivist = ArchivistFactory.getArchivistForType(aModule.getModuleType());
75                 if (aModule.getAlternateDescriptor()!=null) {
76                     // the application is using alternate deployment descriptors
77

78                     // write or copy standard deployment descriptor
79
String JavaDoc ddPath = aModule.getAlternateDescriptor();
80                     DeploymentDescriptorFile ddFile =
81                         moduleArchivist.getStandardDDFile();
82                      
83                     BundleDescriptor bundle =
84                             (BundleDescriptor)aModule.getDescriptor();
85                     if (!bundle.isFullFlag()) {
86                         if (ddFile != null) {
87                             OutputStream JavaDoc os = out.putNextEntry(ddPath);
88                             ddFile.write(bundle, os);
89                             out.closeEntry();
90                         }
91                     } else {
92                         if (aModule.getModuleType().equals(ModuleType.WAR)) {
93                             WebBundleDescriptor webBundle =
94                                 (WebBundleDescriptor) aModule.getDescriptor();
95                             if (webBundle.hasWebServices()) {
96                                 if (ddFile != null) {
97                                     OutputStream JavaDoc os = out.putNextEntry(ddPath);
98                                     ddFile.write(webBundle, os);
99                                     out.closeEntry();
100                                 }
101                             } else {
102                                 Archivist.copyAnEntry(in, out, ddPath);
103                             }
104                         } else {
105                             Archivist.copyAnEntry(in, out, ddPath);
106                         }
107                     }
108
109                     String JavaDoc runtimeDDPath = "sun-" + ddPath;
110                     DeploymentDescriptorFile confDDFile = moduleArchivist.getConfigurationDDFile();
111                     if (confDDFile!=null) {
112                         OutputStream JavaDoc os = out.putNextEntry(runtimeDDPath);
113                         confDDFile.write(aModule.getDescriptor(), os);
114                         out.closeEntry();
115                     }
116                 } else {
117                     AbstractArchive moduleArchive = out.getEmbeddedArchive(aModule.getArchiveUri());
118                     AbstractArchive moduleArchive2 = in.getEmbeddedArchive(aModule.getArchiveUri());
119                     write(aModule.getDescriptor(), moduleArchivist, moduleArchive2, moduleArchive);
120                 }
121             }
122             
123             // now let's write the application descriptor
124
ApplicationArchivist archivist = new ApplicationArchivist();
125             archivist.setDescriptor(application);
126             archivist.writeRuntimeDeploymentDescriptors(out);
127             if (application.isLoadedFromApplicationXml()) {
128                 archivist.copyStandardDeploymentDescriptors(in, out);
129             } else {
130                 archivist.writeStandardDeploymentDescriptors(out);
131             }
132         }
133     }
134     
135     /**
136      * writes a bundle descriptor
137      * @param the bundle descriptor
138      * @param the abstract archive
139      */

140     public void write(BundleDescriptor bundle, AbstractArchive in, AbstractArchive out)
141         throws IOException JavaDoc
142     {
143         Archivist archivist = ArchivistFactory.getArchivistForArchive(out);
144         write(bundle, archivist, in, out);
145     }
146     
147     /**
148      * writes a bundle descriptor
149      * @param the bundle descriptor
150      * @param the archivist responsible for writing such bundle type
151      * @param the archive to write to
152      */

153     protected void write(BundleDescriptor bundle, Archivist archivist, AbstractArchive in, AbstractArchive out)
154         throws IOException JavaDoc
155     {
156         archivist.setDescriptor(bundle);
157         archivist.writeRuntimeDeploymentDescriptors(out);
158         
159         // this path is only called through dynamic deployment
160
// i.e from EJBCompiler.preDeployApp -> BaseManager.saveAppDescriptor
161
// so write out deployment descriptors if it's not a full DD
162
if (!bundle.isFullFlag()) {
163             archivist.writeStandardDeploymentDescriptors(out);
164         } else {
165             // Also if this is a web bundle descriptor, we always want to
166
// rewrite the standard deployment descriptors if we have web
167
// services since the servlet implementation has been switched
168
if (bundle.getModuleType().equals(ModuleType.WAR)) {
169                 WebBundleDescriptor webBundle = (WebBundleDescriptor) bundle;
170                 if (webBundle.hasWebServices()) {
171                     archivist.writeStandardDeploymentDescriptors(out);
172                 } else {
173                     archivist.copyStandardDeploymentDescriptors(in, out);
174                 }
175             } else {
176                 archivist.copyStandardDeploymentDescriptors(in, out);
177             }
178         }
179
180         // copy mapping files if it's not ended with .xml
181
// all xml files will be copied later in BaseManager
182
// (Archivist.copyExtraElements)
183
if (bundle.hasWebServices()) {
184             WebServicesDescriptor webServices = bundle.getWebServices();
185             for (WebService webService : webServices.getWebServices()) {
186                 if (webService.hasMappingFile() &&
187                     !webService.getMappingFileUri().endsWith(".xml")) {
188                     Archivist.copyAnEntry(in, out,
189                     webService.getMappingFileUri());
190                 }
191             }
192         }
193     }
194 }
195
Popular Tags