KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > TargetModule


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.j2ee.deployment.impl;
21
22 import javax.enterprise.deploy.spi.Target JavaDoc;
23 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
24 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
25 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.util.NbBundle;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31
32 /**
33  * @author nn136682
34  */

35 public class TargetModule implements TargetModuleID JavaDoc, java.io.Serializable JavaDoc {
36
37     private static final long serialVersionUID = 69446832504L;
38
39     private final String JavaDoc id;
40     private final String JavaDoc instanceUrl;
41     private final String JavaDoc targetName;
42     private final long timestamp;
43     private final String JavaDoc contentDirectory;
44     private final String JavaDoc contextRoot;
45     private transient TargetModuleID JavaDoc delegate;
46     public static final TargetModuleID JavaDoc[] EMPTY_TMID_ARRAY = new TargetModuleID JavaDoc[0];
47     
48     /** Creates a new instance of TargetModule */
49     public TargetModule(String JavaDoc id, String JavaDoc url, long timestamp, String JavaDoc contentDir, String JavaDoc contextRoot, TargetModuleID JavaDoc delegate) {
50         this(id, url, delegate.getTarget().getName(), timestamp, contentDir, contextRoot);
51         this.delegate = delegate;
52     }
53     
54     public TargetModule(String JavaDoc id, String JavaDoc url, String JavaDoc targetName, long timestamp, String JavaDoc contentDir, String JavaDoc contextRoot) {
55         if (id == null || url == null || targetName == null || timestamp < 0) {
56             java.util.List JavaDoc args = Arrays.asList(new Object JavaDoc[] { id, url, targetName, new Long JavaDoc(timestamp)});
57             throw new IllegalArgumentException JavaDoc(NbBundle.getMessage(TargetModule.class, "MSG_BadTargetModuleAttributes", args));
58         }
59         this.id = id;
60         this.instanceUrl = url;
61         this.targetName = targetName;
62         this.timestamp = timestamp;
63         this.contentDirectory = contentDir;
64         this.contextRoot = (contextRoot == null) ? "" : contextRoot;
65     }
66
67     /* wrapper for map/set operation only */
68     public TargetModule(String JavaDoc id, TargetModuleID JavaDoc delegate) {
69         this(id, "someurl", 0, null, null,delegate);
70     }
71     
72     public String JavaDoc getId() { return id; }
73     public String JavaDoc getInstanceUrl() { return instanceUrl; }
74     public String JavaDoc getTargetName() {
75         if (delegate != null)
76             return delegate.getTarget().getName();
77         else
78             return targetName;
79     }
80     public long getTimestamp() { return timestamp; }
81     //public void setTimestamp(long ts) { this.timestamp = ts; }
82
public String JavaDoc getContentDirectory() {
83         return contentDirectory;
84     }
85     public String JavaDoc getContextRoot() {
86         return contextRoot;
87     }
88     
89     public static class List implements java.io.Serializable JavaDoc {
90         private static final long serialVersionUID = 69446832514L;
91         private TargetModule [] targetModules;
92         public List(TargetModule[] targetModules) {
93             this.targetModules = targetModules;
94         }
95         public List(TargetModule tm) {
96             this.targetModules = new TargetModule[] { tm };
97         }
98         public TargetModule[] getTargetModules() {
99             return targetModules;
100         }
101     }
102
103     public Target JavaDoc findTarget() {
104         ServerInstance instance = ServerRegistry.getInstance().getServerInstance(instanceUrl);
105         return instance.getServerTarget(targetName).getTarget();
106     }
107     
108     //Delegate to TargetModuleID
109
public void initDelegate(ModuleType JavaDoc type) {
110         if (delegate == null) {
111             ServerInstance instance = ServerRegistry.getInstance().getServerInstance(instanceUrl);
112             DeploymentManager JavaDoc dm = instance.getDeploymentManager();
113             Target JavaDoc target = findTarget();
114
115             try {
116                 TargetModuleID JavaDoc[] tmIDs = dm.getAvailableModules(type, new Target JavaDoc[] {target});
117                 for (int i=0; i<tmIDs.length; i++) {
118                     if (id.equals(tmIDs[i].toString())) {
119                         delegate = tmIDs[i];
120                         break;
121                     }
122                 }
123             } catch (Exception JavaDoc e) {
124                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
125             }
126         }
127     }
128     
129     public void initDelegate(TargetModuleID JavaDoc delegate) {
130         this.delegate = delegate;
131     }
132     
133     public static TargetModuleID JavaDoc[] toTargetModuleID(TargetModule[] targetModules) {
134         if (targetModules == null) return new TargetModuleID JavaDoc[0];
135         TargetModuleID JavaDoc [] ret = new TargetModuleID JavaDoc[targetModules.length];
136         for (int i=0; i<ret.length; i++) {
137             ret[i] = targetModules[i].delegate();
138         }
139         return ret;
140     }
141
142     public static Target JavaDoc[] toTarget(TargetModule[] targetModules) {
143         if (targetModules == null) return new Target JavaDoc[0];
144         Target JavaDoc[] ret = new Target JavaDoc[targetModules.length];
145         for (int i=0; i<ret.length; i++) {
146             ret[i] = targetModules[i].delegate().getTarget();
147         }
148         return ret;
149     }
150     
151     public TargetModuleID JavaDoc delegate() {
152         if (delegate == null) {
153             throw new IllegalStateException JavaDoc("Delegate is not set yet"); //NOI18N
154
}
155         return delegate;
156     }
157     public javax.enterprise.deploy.spi.TargetModuleID JavaDoc[] getChildTargetModuleID() {
158         return delegate().getChildTargetModuleID();
159     }
160     public String JavaDoc getModuleID() {
161         return delegate().getModuleID();
162     }
163     public javax.enterprise.deploy.spi.TargetModuleID JavaDoc getParentTargetModuleID() {
164         return delegate().getParentTargetModuleID();
165     }
166     public javax.enterprise.deploy.spi.Target JavaDoc getTarget() {
167         return delegate().getTarget();
168     }
169     public String JavaDoc getWebURL() {
170         return delegate().getWebURL();
171     }
172     public String JavaDoc toString() {
173         if (delegate == null)
174             return id; //issue 37930
175
else
176             return delegate.toString();
177     }
178     public int hashCode() {
179         return getId().hashCode();
180     }
181     public boolean equals(Object JavaDoc obj) {
182         if (obj instanceof TargetModuleID JavaDoc) {
183             TargetModuleID JavaDoc that = (TargetModuleID JavaDoc) obj;
184             return this.getModuleID().equals(that.getModuleID()) && this.getTargetName().equals(that.getTarget().getName());
185         }
186         return false;
187     }
188     //NOTE: this also return list of TM's with delegate only
189
public static java.util.List JavaDoc initDelegate(java.util.List JavaDoc targetModules, java.util.Map JavaDoc delegateTMIDsMap) {
190         ArrayList JavaDoc result = new ArrayList JavaDoc();
191         for (java.util.Iterator JavaDoc i=targetModules.iterator(); i.hasNext();) {
192             TargetModule tm = (TargetModule) i.next();
193             TargetModuleID JavaDoc tmid = (TargetModuleID JavaDoc) delegateTMIDsMap.get(tm.getId());
194             if (tmid != null) {
195                 tm.initDelegate(tmid);
196                 result.add(tm);
197             }
198         }
199         return result;
200     }
201     
202 //----------------------- persistence operations -------------------------------
203
public static java.util.List JavaDoc findByContextRoot(ServerString server, String JavaDoc contextRoot) {
204         String JavaDoc managerDirName = getManagerDirName(server);
205         String JavaDoc[] targetNames = server.getTargets(true);
206         ArrayList JavaDoc targetModules = new ArrayList JavaDoc();
207         for (int i=0; i<targetNames.length; i++) {
208             String JavaDoc targDirName = getReadableName(targetNames[i]);
209             java.util.List JavaDoc tml = TargetModuleConverter.getTargetModulesByContextRoot(managerDirName, targDirName, contextRoot);
210             targetModules.addAll(tml);
211         }
212         return targetModules;
213     }
214     public static void removeByContextRoot(ServerString server, String JavaDoc contextRoot) {
215         java.util.List JavaDoc tms = TargetModule.findByContextRoot(server, contextRoot);
216         for (java.util.Iterator JavaDoc i=tms.iterator(); i.hasNext(); ) {
217             TargetModule tm = (TargetModule) i.next();
218             tm.remove();
219         }
220     }
221     private static String JavaDoc getManagerDirName(ServerString server) {
222         return getReadableName(server.getUrl());
223     }
224     private String JavaDoc getManagerDirName() {
225         return getReadableName(getInstanceUrl());
226     }
227     public static TargetModule[] load(ServerString server, String JavaDoc fileName) {
228         String JavaDoc managerDirName = getManagerDirName(server);
229         String JavaDoc[] targetNames = server.getTargets(true);
230         java.util.List JavaDoc targetModules = new ArrayList JavaDoc();
231         for (int i=0; i<targetNames.length; i++) {
232             String JavaDoc targDirName = getReadableName(targetNames[i]);
233             TargetModule tm = TargetModuleConverter.readTargetModule(managerDirName, targDirName, fileName);
234             if (tm != null) {
235                 targetModules.add(tm);
236             }
237         }
238         return (TargetModule[]) targetModules.toArray(new TargetModule[targetModules.size()]);
239     }
240     public void save(String JavaDoc fileName) {
241         TargetModuleConverter.writeTargetModule(this, getManagerDirName(), getReadableName(targetName), fileName);
242     }
243     public void remove() {
244         String JavaDoc managerDirName = getManagerDirName();
245         String JavaDoc targDirName = getReadableName(targetName);
246         //NOTE: no effect if filename was derived from module folder instead of content folder
247
String JavaDoc fileName = shortNameFromPath(getContentDirectory());
248         TargetModuleConverter.remove(managerDirName, targDirName, fileName);
249     }
250     public static String JavaDoc getReadableName(String JavaDoc s) {
251         int code = s.hashCode();
252         int end = 16;
253         if (end > s.length())
254             end = s.length();
255         StringBuffer JavaDoc sb = TargetModule.subStringBuffer(s, 0, end);
256         sb.append(String.valueOf(code));
257         return sb.toString();
258     }
259     public static StringBuffer JavaDoc subStringBuffer(String JavaDoc s, int start, int end) {
260         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
261         if (end < 0)
262             sb.append(s.substring(start));
263         else
264             sb.append(s.substring(start, end));
265         for (int i=0; i<sb.length(); i++) {
266             if (! Character.isLetterOrDigit(sb.charAt(i)))
267                 sb.setCharAt(i, '_');
268         }
269         return sb;
270     }
271
272     public static String JavaDoc shortNameFromPath(String JavaDoc pathName) {
273         int code = pathName.hashCode();
274         int start = pathName.length()-16;
275         if (start < 0)
276             start = 0;
277         StringBuffer JavaDoc sb = TargetModule.subStringBuffer(pathName, start, -1);
278         sb.append(String.valueOf(code));
279         return sb.toString();
280     }
281 }
282
Popular Tags