KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > appclient > AppClientInfoFactory


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.appclient;
25
26 import com.sun.enterprise.deployment.annotation.AnnotationProcessorException;
27 import com.sun.enterprise.deployment.archivist.AppClientArchivist;
28 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
29 import com.sun.enterprise.deployment.archivist.Archivist;
30 import com.sun.enterprise.deployment.archivist.ArchivistFactory;
31 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
32 import com.sun.enterprise.deployment.deploy.shared.ArchiveFactory;
33 import com.sun.enterprise.util.i18n.StringManager;
34 import java.io.File JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.net.URISyntaxException JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39 import org.xml.sax.SAXParseException JavaDoc;
40
41 /**
42  * Factory class for creating the appropriate subtype of AppClientInfo based
43  * on various factors.
44  *
45  * @author tjquinn
46  */

47 public class AppClientInfoFactory {
48
49     /** access to the localizable strings */
50     protected static StringManager localStrings =
51                         StringManager.getManager(AppClientInfoFactory.class);
52     
53     /**
54      *Factory merhod that creates an object of the correct concrete type
55      *for the given location and content.
56      *@param locationFile File where the client is
57      *@param mainClassFromCommandLine the main class as specified on the command line
58      */

59     public static AppClientInfo buildAppClientInfo(
60             boolean isJWS,
61             Logger JavaDoc logger,
62             File JavaDoc locationFile,
63             String JavaDoc mainClassFromCommandLine,
64             String JavaDoc displayNameFromCommandLine,
65             String JavaDoc classFileFromCommandLine)
66                 throws IOException JavaDoc, SAXParseException JavaDoc, ClassNotFoundException JavaDoc,
67                        URISyntaxException JavaDoc, AnnotationProcessorException,
68                        Exception JavaDoc {
69         AppClientInfo result = null;
70
71         /*
72          *Check if the user specified a .class file on the command line.
73          */

74         if (classFileFromCommandLine != null) {
75             /*
76              *Yes, it's a .class file. Use an app client archivist and, from
77              *it, get the default app client descriptor. Then create the
78              *new app client info instance.
79              */

80             Archivist archivist = new AppClientArchivist();
81             result = new ClassFileAppClientInfo(
82                     isJWS,
83                     logger,
84                     locationFile,
85                     archivist,
86                     mainClassFromCommandLine,
87                     classFileFromCommandLine);
88         } else {
89             /*
90              *The user did not specify a .class file on the command line, so
91              *the locationFile argument refers to a valid module.
92              *Construct an Archivist for the location file.
93              */

94             Archivist archivist = prepareArchivist(locationFile);
95             
96             if (archivist != null) {
97                 /*
98                  *Choose which type of concrete AppClientInfo class is
99                  *suitable for this app client execution.
100                  */

101                 if (archivist instanceof AppClientArchivist) {
102                     result = new StandAloneAppClientInfo(
103                             isJWS,
104                             logger,
105                             locationFile,
106                             archivist,
107                             mainClassFromCommandLine);
108                 } else if (archivist instanceof ApplicationArchivist) {
109                     /*
110                      *The descriptor should be of an application if it is not an
111                      *app client descriptor.
112                      */

113                     result = new NestedAppClientInfo(
114                             isJWS,
115                             logger,
116                             locationFile,
117                             archivist,
118                             mainClassFromCommandLine,
119                             displayNameFromCommandLine);
120                 } else {
121                     /*
122                      *The archivist factory recognized the archive as a valid
123                      *one but it is not an app client or an application.
124                      *Reject it.
125                      */

126                     throw new IllegalArgumentException JavaDoc(localStrings.getString("appclient.unexpectedArchive", locationFile.getAbsolutePath()));
127                 }
128             } else {
129                 /*
130                  *The archivist is null, which means the user-provided location is
131                  *not recognized as a known type of module.
132                  */

133                 throw new IllegalArgumentException JavaDoc(localStrings.getString("appclient.invalidArchive", locationFile.getAbsolutePath()));
134             }
135         }
136         result.completeInit();
137         if (logger.isLoggable(Level.FINE)) {
138             logger.fine(result.toString());
139         }
140         return result;
141     }
142
143     /**
144      *Returns an archivist of the correct concrete type (app client or application)
145      *given the contents of the archive.
146      *@param archive the archive that contains the module
147      *@param className
148      *@return concrete Archivist of the correct type given the contents of the archive
149      *@exeception IOException in case of error getting an archivist for the archive
150      */

151     private static Archivist prepareArchivist(File JavaDoc file) throws IOException JavaDoc {
152         Archivist result = null;
153         result = ArchivistFactory.getArchivistForArchive(file);
154         return result;
155     }
156 }
157
Popular Tags