1 28 29 package com.caucho.jca; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.lifecycle.Lifecycle; 33 import com.caucho.server.deploy.DeployController; 34 import com.caucho.server.util.CauchoSystem; 35 import com.caucho.util.L10N; 36 import com.caucho.vfs.Path; 37 import com.caucho.vfs.Vfs; 38 39 import javax.annotation.PostConstruct; 40 import java.io.IOException ; 41 import java.util.ArrayList ; 42 import java.util.HashSet ; 43 import java.util.Iterator ; 44 45 48 public class ResourceDeploy { 49 private static final L10N L = new L10N(ResourceDeploy.class); 50 51 private final ResourceDeployAdmin _admin = new ResourceDeployAdmin(this); 52 53 private Path _containerRootDirectory; 54 55 private Path _rarDir; 56 private Path _rarExpandDir; 57 58 private String _expandPrefix = ""; 59 60 private HashSet <String > _rarNames = new HashSet <String >(); 61 62 private volatile boolean _isInit; 63 64 public ResourceDeploy() 65 { 66 setExpandPrefix("_rar_"); 67 68 _containerRootDirectory = Vfs.getPwd(); 69 70 } 71 72 Path getContainerRootDirectory() 73 { 74 return _containerRootDirectory; 75 } 76 77 80 public Path getPath() 81 { 82 return _rarDir; 83 } 84 85 88 public void setPath(Path path) 89 { 90 _rarDir = path; 91 } 92 93 public Path getArchiveDirectory() 94 { 95 return getPath(); 96 } 97 98 public Path getArchivePath(String name) 99 { 100 return getArchiveDirectory().lookup(name + getExtension()); 101 } 102 103 109 protected String getExpandName(String name) 110 { 111 return getExpandPrefix() + name + getExpandSuffix(); 112 } 113 114 115 118 public void setExpandPath(Path path) 119 { 120 _rarExpandDir = path; 121 } 122 123 126 public Path getExpandPath() 127 { 128 return getExpandDirectory(); 129 } 130 131 134 public Path getExpandDirectory() 135 { 136 if (_rarExpandDir != null) 137 return _rarExpandDir; 138 else 139 return _rarDir; 140 } 141 142 148 public Path getExpandPath(String name) 149 { 150 if (!isDeployedKey(name)) 151 return null; 152 153 return getExpandDirectory().lookup(getExpandName(name)); 154 } 155 156 private boolean isDeployedKey(String name) 157 { 158 return _rarNames.contains(name); 159 } 160 161 164 public String getExpandPrefix() 165 { 166 return _expandPrefix; 167 } 168 169 172 public void setExpandPrefix(String prefix) 173 { 174 _expandPrefix = prefix; 175 } 176 177 public boolean isModified() 178 { 179 try { 180 return ! _rarNames.equals(getRarNames()); 181 } catch (Exception e) { 182 return false; 183 } 184 } 185 186 189 @PostConstruct 190 public void init() 191 throws ConfigException 192 { 193 synchronized (this) { 194 if (_isInit) 195 return; 196 _isInit = true; 197 } 198 199 if (getPath() == null) 200 throw new ConfigException(L.l("resource-deploy requires a path attribute")); 201 202 try { 203 _rarNames = getRarNames(); 204 205 Iterator <String > iter = _rarNames.iterator(); 206 while (iter.hasNext()) { 207 String name = iter.next(); 208 209 ResourceArchive rar; 210 211 rar = new ResourceArchive(); 212 rar.setRarPath(getPath().lookup(name + ".rar")); 213 rar.setRootDirectory(getExpandPath().lookup(getExpandPrefix() + name)); 214 215 Path oldPwd = Vfs.getPwd(); 216 217 try { 218 Vfs.setPwd(rar.getRootDirectory()); 219 220 for (ResourceConfig config : ResourceDefault.getDefaultList()) { 221 config.getBuilderProgram().configure(rar); 222 } 223 } finally { 224 Vfs.setPwd(oldPwd); 225 } 226 227 rar.init(); 228 229 ResourceArchiveManager.addResourceArchive(rar); 230 } 231 } catch (ConfigException e) { 232 throw e; 233 } catch (Throwable e) { 234 throw new ConfigException(e); 235 } 236 237 _admin.register(); 238 } 239 240 243 private HashSet <String > getRarNames() 244 throws IOException 245 { 246 HashSet <String > rarNames = new HashSet <String >(); 247 248 Path rarDir = getPath(); 249 Path rarExpandDir = getExpandPath(); 250 251 if (rarDir == null || rarExpandDir == null) 252 return rarNames; 253 254 String []rarDirList = rarDir.list(); 255 256 loop: 258 for (int i = 0; i < rarDirList.length; i++) { 259 String rarName = rarDirList[i]; 260 String appName; 261 262 if (! rarName.endsWith(".rar")) 263 continue; 264 265 Path path = rarDir.lookup(rarName); 266 267 if (! path.canRead()) 268 continue; 269 270 appName = rarName.substring(0, rarName.length() - 4); 271 272 if (CauchoSystem.isCaseInsensitive()) 273 appName = appName.toLowerCase(); 274 275 rarNames.add(appName); 276 } 277 278 String []rarExpandList = rarExpandDir.list(); 279 ArrayList <String > newNames = new ArrayList <String >(); 280 281 loop: 283 for (int i = 0; i < rarExpandList.length; i++) { 284 String rarDirName = rarExpandList[i]; 285 286 if (! rarDirName.startsWith(getExpandPrefix())) 287 continue; 288 289 if (CauchoSystem.isCaseInsensitive()) 290 rarDirName = rarDirName.toLowerCase(); 291 292 Path path = rarExpandDir.lookup(rarDirName); 293 294 if (! path.isDirectory() || ! rarDirName.startsWith(getExpandPrefix())) 295 continue; 296 297 String appName = rarDirName.substring(getExpandPrefix().length()); 298 299 if (! newNames.contains(appName)) 300 newNames.add(appName); 301 302 rarNames.add(appName); 303 } 304 305 return rarNames; 306 } 307 308 public String getExtension() 309 { 310 return ".rar"; 311 } 312 313 public String getExpandSuffix() 314 { 315 return ""; 316 } 317 318 public long getDependencyCheckInterval() 319 { 320 return -1; 321 } 322 323 public String getStartupMode() 324 { 325 return DeployController.STARTUP_AUTOMATIC; 326 } 327 328 public String getRedeployMode() 329 { 330 return DeployController.REDEPLOY_MANUAL; 331 } 332 333 public String getState() 334 { 335 if (!_isInit) 336 return Lifecycle.getStateName(Lifecycle.IS_NEW); 337 else 338 return Lifecycle.getStateName(Lifecycle.IS_ACTIVE); 339 } 340 341 public void start() 342 { 343 } 345 346 public Throwable getConfigException() 347 { 348 return null; 350 } 351 352 public void stop() 353 { 354 } 356 357 public void update() 358 { 359 } 361 362 public String [] getNames() 363 { 364 return new String [0]; 366 } 367 368 public void stop(String name) 369 { 370 } 372 373 public void start(String name) 374 { 375 } 377 378 public Throwable getConfigException(String moduleID) 379 { 380 return null; 382 } 383 384 public void undeploy(String name) 385 { 386 } 388 389 } 390 | Popular Tags |