1 19 20 package org.netbeans.modules.j2ee.deployment.impl; 21 22 import javax.enterprise.deploy.spi.Target ; 23 import javax.enterprise.deploy.spi.TargetModuleID ; 24 import javax.enterprise.deploy.spi.DeploymentManager ; 25 import javax.enterprise.deploy.shared.ModuleType ; 26 import org.openide.ErrorManager; 27 import org.openide.util.NbBundle; 28 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 32 35 public class TargetModule implements TargetModuleID , java.io.Serializable { 36 37 private static final long serialVersionUID = 69446832504L; 38 39 private final String id; 40 private final String instanceUrl; 41 private final String targetName; 42 private final long timestamp; 43 private final String contentDirectory; 44 private final String contextRoot; 45 private transient TargetModuleID delegate; 46 public static final TargetModuleID [] EMPTY_TMID_ARRAY = new TargetModuleID [0]; 47 48 49 public TargetModule(String id, String url, long timestamp, String contentDir, String contextRoot, TargetModuleID delegate) { 50 this(id, url, delegate.getTarget().getName(), timestamp, contentDir, contextRoot); 51 this.delegate = delegate; 52 } 53 54 public TargetModule(String id, String url, String targetName, long timestamp, String contentDir, String contextRoot) { 55 if (id == null || url == null || targetName == null || timestamp < 0) { 56 java.util.List args = Arrays.asList(new Object [] { id, url, targetName, new Long (timestamp)}); 57 throw new IllegalArgumentException (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 68 public TargetModule(String id, TargetModuleID delegate) { 69 this(id, "someurl", 0, null, null,delegate); 70 } 71 72 public String getId() { return id; } 73 public String getInstanceUrl() { return instanceUrl; } 74 public String getTargetName() { 75 if (delegate != null) 76 return delegate.getTarget().getName(); 77 else 78 return targetName; 79 } 80 public long getTimestamp() { return timestamp; } 81 public String getContentDirectory() { 83 return contentDirectory; 84 } 85 public String getContextRoot() { 86 return contextRoot; 87 } 88 89 public static class List implements java.io.Serializable { 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 findTarget() { 104 ServerInstance instance = ServerRegistry.getInstance().getServerInstance(instanceUrl); 105 return instance.getServerTarget(targetName).getTarget(); 106 } 107 108 public void initDelegate(ModuleType type) { 110 if (delegate == null) { 111 ServerInstance instance = ServerRegistry.getInstance().getServerInstance(instanceUrl); 112 DeploymentManager dm = instance.getDeploymentManager(); 113 Target target = findTarget(); 114 115 try { 116 TargetModuleID [] tmIDs = dm.getAvailableModules(type, new Target [] {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 e) { 124 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 125 } 126 } 127 } 128 129 public void initDelegate(TargetModuleID delegate) { 130 this.delegate = delegate; 131 } 132 133 public static TargetModuleID [] toTargetModuleID(TargetModule[] targetModules) { 134 if (targetModules == null) return new TargetModuleID [0]; 135 TargetModuleID [] ret = new TargetModuleID [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 [] toTarget(TargetModule[] targetModules) { 143 if (targetModules == null) return new Target [0]; 144 Target [] ret = new Target [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 delegate() { 152 if (delegate == null) { 153 throw new IllegalStateException ("Delegate is not set yet"); } 155 return delegate; 156 } 157 public javax.enterprise.deploy.spi.TargetModuleID [] getChildTargetModuleID() { 158 return delegate().getChildTargetModuleID(); 159 } 160 public String getModuleID() { 161 return delegate().getModuleID(); 162 } 163 public javax.enterprise.deploy.spi.TargetModuleID getParentTargetModuleID() { 164 return delegate().getParentTargetModuleID(); 165 } 166 public javax.enterprise.deploy.spi.Target getTarget() { 167 return delegate().getTarget(); 168 } 169 public String getWebURL() { 170 return delegate().getWebURL(); 171 } 172 public String toString() { 173 if (delegate == null) 174 return id; else 176 return delegate.toString(); 177 } 178 public int hashCode() { 179 return getId().hashCode(); 180 } 181 public boolean equals(Object obj) { 182 if (obj instanceof TargetModuleID ) { 183 TargetModuleID that = (TargetModuleID ) obj; 184 return this.getModuleID().equals(that.getModuleID()) && this.getTargetName().equals(that.getTarget().getName()); 185 } 186 return false; 187 } 188 public static java.util.List initDelegate(java.util.List targetModules, java.util.Map delegateTMIDsMap) { 190 ArrayList result = new ArrayList (); 191 for (java.util.Iterator i=targetModules.iterator(); i.hasNext();) { 192 TargetModule tm = (TargetModule) i.next(); 193 TargetModuleID tmid = (TargetModuleID ) delegateTMIDsMap.get(tm.getId()); 194 if (tmid != null) { 195 tm.initDelegate(tmid); 196 result.add(tm); 197 } 198 } 199 return result; 200 } 201 202 public static java.util.List findByContextRoot(ServerString server, String contextRoot) { 204 String managerDirName = getManagerDirName(server); 205 String [] targetNames = server.getTargets(true); 206 ArrayList targetModules = new ArrayList (); 207 for (int i=0; i<targetNames.length; i++) { 208 String targDirName = getReadableName(targetNames[i]); 209 java.util.List tml = TargetModuleConverter.getTargetModulesByContextRoot(managerDirName, targDirName, contextRoot); 210 targetModules.addAll(tml); 211 } 212 return targetModules; 213 } 214 public static void removeByContextRoot(ServerString server, String contextRoot) { 215 java.util.List tms = TargetModule.findByContextRoot(server, contextRoot); 216 for (java.util.Iterator i=tms.iterator(); i.hasNext(); ) { 217 TargetModule tm = (TargetModule) i.next(); 218 tm.remove(); 219 } 220 } 221 private static String getManagerDirName(ServerString server) { 222 return getReadableName(server.getUrl()); 223 } 224 private String getManagerDirName() { 225 return getReadableName(getInstanceUrl()); 226 } 227 public static TargetModule[] load(ServerString server, String fileName) { 228 String managerDirName = getManagerDirName(server); 229 String [] targetNames = server.getTargets(true); 230 java.util.List targetModules = new ArrayList (); 231 for (int i=0; i<targetNames.length; i++) { 232 String 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 fileName) { 241 TargetModuleConverter.writeTargetModule(this, getManagerDirName(), getReadableName(targetName), fileName); 242 } 243 public void remove() { 244 String managerDirName = getManagerDirName(); 245 String targDirName = getReadableName(targetName); 246 String fileName = shortNameFromPath(getContentDirectory()); 248 TargetModuleConverter.remove(managerDirName, targDirName, fileName); 249 } 250 public static String getReadableName(String s) { 251 int code = s.hashCode(); 252 int end = 16; 253 if (end > s.length()) 254 end = s.length(); 255 StringBuffer sb = TargetModule.subStringBuffer(s, 0, end); 256 sb.append(String.valueOf(code)); 257 return sb.toString(); 258 } 259 public static StringBuffer subStringBuffer(String s, int start, int end) { 260 StringBuffer sb = new StringBuffer (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 shortNameFromPath(String pathName) { 273 int code = pathName.hashCode(); 274 int start = pathName.length()-16; 275 if (start < 0) 276 start = 0; 277 StringBuffer sb = TargetModule.subStringBuffer(pathName, start, -1); 278 sb.append(String.valueOf(code)); 279 return sb.toString(); 280 } 281 } 282 | Popular Tags |