KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > archivist > PluggableArchivistsHelper


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 /*
25  * PluggableArchivistsHelper.java
26  *
27  * Created on June 4, 2004, 10:48 AM
28  */

29
30 package com.sun.enterprise.deployment.archivist;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
37
38 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
39 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
40 import com.sun.enterprise.deployment.deploy.shared.InputJarArchive;
41 import com.sun.enterprise.deployment.util.DOLUtils;
42 import com.sun.enterprise.util.i18n.StringManager;
43
44
45 /**
46  *
47  * @author Jerome Dochez
48  */

49 public class PluggableArchivistsHelper implements PluggableArchivists {
50     
51     // All archivist should be registered here with an empty instance
52
// I could have used a prototype or some factory classes but it would
53
// have been an overkill. Instances are registered so I can use the
54
// abstract methods defined at the Archivist level.
55
private Archivist[] archivists = new Archivist[0];
56
57     /** string manager */
58     private static StringManager localStrings =
59         StringManager.getManager( PluggableArchivistsHelper.class );
60
61     
62     /** Creates a new instance of PluggableArchivistsHelper */
63     public PluggableArchivistsHelper() {
64     }
65     
66     /**
67      * @return a new Archivist implementation for the archive file type
68      * Supported J2EE modules are defined in the J2EE platform spec
69      */

70     public Archivist getArchivistForArchive(String JavaDoc path) throws IOException JavaDoc {
71         File JavaDoc f = new File JavaDoc(path);
72         if (!f.exists()) {
73             throw new FileNotFoundException JavaDoc(path);
74         }
75         AbstractArchive archive;
76         if (f.isDirectory()) {
77             archive = new FileArchive();
78             ((FileArchive) archive).open(path);
79         } else {
80             archive = new InputJarArchive();
81             ((InputJarArchive) archive).open(path);
82         }
83         Archivist archivist=null;
84         try {
85             archivist = getArchivistForArchive(archive);
86         } finally {
87             archive.close();
88         }
89         return archivist;
90     }
91     
92     /**
93      * @return a new Archivist implementation for the archive file type
94      * Supported J2EE modules are defined in the J2EE platform spec
95      */

96     public Archivist getArchivistForArchive(File JavaDoc jarFileOrDirectory) throws IOException JavaDoc {
97         return getArchivistForArchive(jarFileOrDirectory.getAbsolutePath());
98     }
99
100     
101     /**
102      * @return a new Archivist implementation for the archive file type
103      * Supported J2EE modules are defined in the J2EE platform spec
104      */

105     public Archivist getArchivistForArchive(AbstractArchive archive) throws IOException JavaDoc {
106
107         Archivist a = handles(archive);
108         if (a != null) {
109             try {
110                 Archivist archivist = (Archivist) a.getClass().newInstance();
111                 archivist.setPluggableArchivists(this);
112                 return archivist;
113             } catch (Exception JavaDoc e) {
114                 DOLUtils.getDefaultLogger().log(
115                 Level.SEVERE,
116                 "enterprise.deployment.backend.archivistInstantiationFailure",
117                 new Object JavaDoc[] {a.getClass(), archive});
118                 e.printStackTrace();
119             }
120         } else {
121             String JavaDoc msg = localStrings.getString(
122                 "enterprise.deployment.unknown.application.type",
123                 archive.getArchiveUri());
124             throw new IOException JavaDoc(msg);
125         }
126         return null;
127     }
128
129     private Archivist handles(AbstractArchive archive) throws IOException JavaDoc {
130
131         //first, check the existence of any deployment descriptors
132
for (Archivist a : archivists) {
133             if (a.hasStandardDeploymentDescriptor(archive) ||
134                     a.hasRuntimeDeploymentDescriptor(archive)) {
135                 return a;
136             }
137         }
138
139         // Java EE 5 Specification: Section EE.8.4.2.1
140

141         //second, check file extension if any, excluding .jar as it needs
142
//additional processing
143
String JavaDoc uri = archive.getArchiveUri();
144         File JavaDoc file = new File JavaDoc(uri);
145         if (!file.isDirectory() && !uri.endsWith(Archivist.EJB_EXTENSION)) {
146             for (Archivist a : archivists) {
147                 if (uri.endsWith(a.getArchiveExtension())) {
148                     return a;
149                 }
150             }
151         }
152
153         //finally, still not returned here, call for additional processing
154
for (Archivist a : archivists) {
155             if (a.postHandles(archive)) {
156                 return a;
157             }
158         }
159
160         return null;
161     }
162
163     /**
164      * @return a new Archivist implementation for the type passed.
165      * Supported types are defined in the application.xml DTD
166      */

167     public Archivist getArchivistForType(ModuleType JavaDoc type) {
168         
169         for (int i=0;i<archivists.length;i++) {
170             if (archivists[i].getModuleType().equals(type)) {
171                 try {
172                     Archivist archivist = (Archivist) archivists[i].getClass().newInstance();
173                     archivist.setPluggableArchivists(this);
174                     return archivist;
175                 } catch (Exception JavaDoc e) {
176                     DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.archivistInstantiationFailure",
177                                 new Object JavaDoc[] {archivists[i].getClass(), type});
178                     e.printStackTrace();
179                 }
180             }
181         }
182         DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.archivistInstantiationFailure",
183                                 new Object JavaDoc[] {null, type});
184         return null;
185     }
186     
187     /**
188      * register a new type of archivist
189      * @param Archivist to register...
190      */

191     public void registerArchivist(Archivist archivist) {
192         for (int i=0;i<archivists.length;i++) {
193             if (archivists[i].getModuleType().equals(archivist.getModuleType())) {
194                 // we are replacing an archivist
195
archivists[i]=archivist;
196                 return;
197             }
198         }
199         // if we end up here, it's a new archvist for a new type of archive...
200
Archivist[] newArchivists = new Archivist[archivists.length+1];
201         System.arraycopy(archivists, 0, newArchivists, 0, archivists.length);
202         newArchivists[archivists.length]=archivist;
203         archivists = newArchivists;
204     }
205     
206     /**
207      * @return the array of registered archivists
208      */

209     public Archivist[] getRegisteredArchivists() {
210         Archivist[] newArchivists = new Archivist[archivists.length+1];
211         System.arraycopy(archivists, 0, newArchivists, 0, archivists.length);
212         return newArchivists;
213     }
214      
215 }
216
Popular Tags