KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > TomcatFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5;
21
22 import java.util.WeakHashMap JavaDoc;
23 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
24 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
25 import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException JavaDoc;
26 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
27 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
28 import org.netbeans.modules.tomcat5.TomcatManager.TomcatVersion;
29 import org.openide.ErrorManager;
30 import org.openide.util.NbBundle;
31
32 /**
33  * Factory capable to create DeploymentManager that can deploy to Tomcat 5 and 6.
34  *
35  * Tomcat URI has following format:
36  * <PRE><CODE>tomcat[55|60]:[home=&lt;home_path&gt;:[base=&lt;base_path&gt;:]]&lt;manager_app_url&gt;</CODE></PRE>
37  * for example
38  * <PRE><CODE>tomcat:http://localhost:8080/manager/</CODE></PRE>
39  * where paths values will be used as CATALINA_HOME and CATALINA_BASE properties and manager_app_url
40  * denotes URL of manager application configured on this server and has to start with <CODE>http:</CODE>.
41  * @author Radim Kubacki
42  */

43 public final class TomcatFactory implements DeploymentFactory JavaDoc {
44     
45     public static final String JavaDoc SERVER_ID_50 = "Tomcat"; // NOI18N
46
public static final String JavaDoc SERVER_ID_55 = "Tomcat55"; // NOI18N
47
public static final String JavaDoc SERVER_ID_60 = "Tomcat60"; // NOI18N
48

49     public static final String JavaDoc TOMCAT_URI_PREFIX_50 = "tomcat:"; // NOI18N
50
public static final String JavaDoc TOMCAT_URI_PREFIX_55 = "tomcat55:"; // NOI18N
51
public static final String JavaDoc TOMCAT_URI_PREFIX_60 = "tomcat60:"; // NOI18N
52

53     private static final String JavaDoc DISCONNECTED_URI_50 = "tomcat:jakarta-tomcat-5.0.x"; // NOI18N
54
private static final String JavaDoc DISCONNECTED_URI_55 = "tomcat55:jakarta-tomcat-5.5.x"; // NOI18N
55
private static final String JavaDoc DISCONNECTED_URI_60 = "tomcat60:apache-tomcat-6.0.x"; // NOI18N
56

57     private static TomcatFactory instance;
58     private static TomcatFactory instance55;
59     private static TomcatFactory instance60;
60     
61     private static final WeakHashMap JavaDoc managerCache = new WeakHashMap JavaDoc();
62     
63     private static ErrorManager err = ErrorManager.getDefault ().getInstance ("org.netbeans.modules.tomcat5"); // NOI18N
64

65     private final String JavaDoc tomcatUriPrefix;
66     private final String JavaDoc disconnectedUri;
67     private final TomcatVersion version;
68             
69     private TomcatFactory(TomcatVersion version) {
70         this.version = version;
71         switch (version) {
72             case TOMCAT_50 :
73                 tomcatUriPrefix = TOMCAT_URI_PREFIX_50;
74                 disconnectedUri = DISCONNECTED_URI_50;
75                 break;
76             case TOMCAT_55 :
77                 tomcatUriPrefix = TOMCAT_URI_PREFIX_55;
78                 disconnectedUri = DISCONNECTED_URI_55;
79                 break;
80             case TOMCAT_60 :
81             default:
82                 tomcatUriPrefix = TOMCAT_URI_PREFIX_60;
83                 disconnectedUri = DISCONNECTED_URI_60;
84                 break;
85         }
86     }
87     
88     /**
89      * Factory method to create DeploymentFactory for Tomcat 5.0.x
90      */

91     public static synchronized TomcatFactory create50() {
92         if (instance == null) {
93             if (err.isLoggable (ErrorManager.INFORMATIONAL)) err.log ("Creating TomcatFactory"); // NOI18N
94
instance = new TomcatFactory(TomcatVersion.TOMCAT_50);
95             DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance);
96         }
97         return instance;
98     }
99     
100     /**
101      * Factory method to create DeploymentFactory for Tomcat 5.5.x
102      */

103     public static synchronized TomcatFactory create55() {
104         if (instance55 == null) {
105             if (err.isLoggable (ErrorManager.INFORMATIONAL)) err.log ("Creating TomcatFactory"); // NOI18N
106
instance55 = new TomcatFactory(TomcatVersion.TOMCAT_55);
107             DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance55);
108         }
109         return instance55;
110     }
111     
112     /**
113      * Factory method to create DeploymentFactory for Tomcat 6.0.x
114      */

115     public static synchronized TomcatFactory create60() {
116         if (instance60 == null) {
117             if (err.isLoggable (ErrorManager.INFORMATIONAL)) err.log ("Creating TomcatFactory"); // NOI18N
118
instance60 = new TomcatFactory(TomcatVersion.TOMCAT_60);
119             DeploymentFactoryManager.getInstance().registerDeploymentFactory(instance60);
120         }
121         return instance60;
122     }
123     
124     /** Get the {@link org.openide.ErrorManager} that logs module events.
125      * @return Module specific ErrorManager.
126      */

127     public static ErrorManager getEM () {
128         return err;
129     }
130     
131     /** Factory method to create DeploymentManager.
132      * @param uri URL of configured manager application.
133      * @param uname user with granted manager role
134      * @param passwd user's password
135      * @throws DeploymentManagerCreationException
136      * @return {@link TomcatManager}
137      */

138     public synchronized DeploymentManager JavaDoc getDeploymentManager(String JavaDoc uri, String JavaDoc uname, String JavaDoc passwd)
139     throws DeploymentManagerCreationException JavaDoc {
140         if (!handlesURI (uri)) {
141             throw new DeploymentManagerCreationException JavaDoc ("Invalid URI:" + uri); // NOI18N
142
}
143         // Lets reuse the same instance of TomcatManager for each server instance
144
// during the IDE session, j2eeserver does not ensure this. Without it,
145
// however, we could not rely on keeping data in the member variables.
146
InstanceProperties ip = InstanceProperties.getInstanceProperties(uri);
147         if (ip == null) {
148             // null ip either means that the instance is not registered, or that this is the disconnected URL
149
if (!disconnectedUri.equals(uri)) {
150                 throw new DeploymentManagerCreationException JavaDoc("Tomcat instance: " + uri + " is not registered in the IDE."); // NOI18N
151
}
152         }
153         TomcatManager tm = (TomcatManager)managerCache.get(ip);
154         if (tm == null) {
155             try {
156                 tm = new TomcatManager(true, uri.substring(tomcatUriPrefix.length()), version);
157                 managerCache.put(ip, tm);
158             } catch (IllegalArgumentException JavaDoc iae) {
159                 Throwable JavaDoc t = new DeploymentManagerCreationException JavaDoc("Cannot create deployment manager for Tomcat instance: " + uri + "."); // NOI18N
160
throw (DeploymentManagerCreationException JavaDoc)(t.initCause(iae));
161             }
162         }
163         return tm;
164     }
165     
166     public DeploymentManager JavaDoc getDisconnectedDeploymentManager(String JavaDoc uri)
167     throws DeploymentManagerCreationException JavaDoc {
168         // no need to distinguish beetween the connected and disconnected DM for Tomcat
169
return getDeploymentManager(uri, null, null);
170     }
171     
172     public String JavaDoc getDisplayName() {
173         switch (version) {
174             case TOMCAT_50 :
175                 return NbBundle.getMessage(TomcatFactory.class, "LBL_TomcatFactory");
176             case TOMCAT_55 :
177                 return NbBundle.getMessage(TomcatFactory.class, "LBL_TomcatFactory55");
178             case TOMCAT_60 :
179             default:
180                 return NbBundle.getMessage(TomcatFactory.class, "LBL_TomcatFactory60");
181         }
182     }
183     
184     public String JavaDoc getProductVersion() {
185         return NbBundle.getMessage(TomcatFactory.class, "LBL_TomcatFactoryVersion");
186     }
187     
188     /**
189      * @param str
190      * @return <CODE>true</CODE> for URIs beggining with <CODE>tomcat[55|60]:</CODE> prefix
191      */

192     public boolean handlesURI(String JavaDoc str) {
193         return str != null && str.startsWith (tomcatUriPrefix);
194     }
195     
196 }
197
Popular Tags