1 19 20 21 package org.netbeans.modules.j2ee.deployment.impl; 22 23 import org.netbeans.modules.j2ee.deployment.plugins.api.*; 24 import org.netbeans.modules.j2ee.deployment.devmodules.api.*; 25 import org.netbeans.modules.j2ee.deployment.execution.DeploymentTarget; 26 import org.netbeans.modules.j2ee.deployment.execution.DeploymentConfigurationProvider; 27 28 import javax.enterprise.deploy.spi.DeploymentConfiguration ; 29 import javax.enterprise.deploy.spi.Target ; 30 import javax.enterprise.deploy.model.DeployableObject ; 31 import org.openide.util.NbBundle; 32 import javax.enterprise.deploy.shared.CommandType ; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileLock; 36 import org.openide.filesystems.FileUtil; 37 38 import java.util.*; 39 import java.io.*; 40 41 45 public class InitialServerFileDistributor extends ServerProgress { 46 ServerString serverString; 47 DeploymentTarget dtarget; 48 IncrementalDeployment incDeployment; 49 Target target; 50 boolean inPlace = false; 51 52 53 public InitialServerFileDistributor(DeploymentTarget dtarget, Target target) { 54 super(dtarget.getServer().getServerInstance()); 55 this.serverString = dtarget.getServer(); 56 this.dtarget = dtarget; 57 this.target = target; 58 incDeployment = serverString.getServerInstance().getIncrementalDeployment (); 59 } 60 61 public File distribute() { 62 DeploymentConfigurationProvider deployment = dtarget.getDeploymentConfigurationProvider(); 63 J2eeModule source = dtarget.getModule(); 64 DeployableObject deployable = deployment.getDeployableObject(null); 65 String name = dtarget.getDeploymentName(); 66 File dir = incDeployment.getDirectoryForNewApplication (name, target, deployment.getDeploymentConfiguration ()); 67 try { 68 if (dir == null) { 69 inPlace = true; 70 if (dtarget.getModule().getContentDirectory() != null) { 71 dir = FileUtil.toFile(dtarget.getModule().getContentDirectory()); 72 } 73 if (dir == null) { 74 String msg = NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_InPlaceNoSupport"); 75 setStatusDistributeFailed(msg); 76 return null; 77 } else { 78 setStatusDistributeCompleted(NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_InPlaceDeployment", dir)); return dir; 80 } 81 } 82 83 setStatusDistributeRunning(NbBundle.getMessage( 84 InitialServerFileDistributor.class, "MSG_RunningInitialDeploy", dtarget.getDeploymentName(), dir)); 85 86 _distribute(source.getArchiveContents(), dir, null); 87 88 if (source instanceof J2eeModuleContainer) { 89 J2eeModule[] childModules = ((J2eeModuleContainer)source).getModules(null); 90 for (int i=0; i<childModules.length; i++) { 91 String uri = childModules[i].getUrl(); 92 DeployableObject childModule = deployment.getDeployableObject(uri); 93 File subdir = incDeployment.getDirectoryForNewModule(dir, uri, childModule, deployment.getDeploymentConfiguration ()); 94 _distribute(childModules[i].getArchiveContents(), subdir, uri); 95 } 96 } 97 98 setStatusDistributeCompleted(NbBundle.getMessage( 99 InitialServerFileDistributor.class, "MSG_DoneInitialDistribute", dtarget.getDeploymentName())); 100 101 return dir; 102 103 } catch (Exception e) { 104 setStatusDistributeFailed(e.getMessage()); 105 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 106 if (!inPlace && !cleanup (dir)) { 107 setStatusDistributeFailed ("Failed to cleanup the data after unsucesful distribution"); 108 } 109 } 110 return null; 111 } 112 113 public void cleanup () { 114 if (inPlace) 115 return; 116 117 DeploymentConfigurationProvider deployment = dtarget.getDeploymentConfigurationProvider(); 118 J2eeModule source = dtarget.getModule(); 119 DeployableObject deployable = deployment.getDeployableObject(null); 120 File dir = incDeployment.getDirectoryForNewApplication (target, deployable, deployment.getDeploymentConfiguration ()); 121 if (!cleanup (dir)) { 122 setStatusDistributeFailed ("Failed to cleanup the data after unsucesful distribution"); 123 } 124 } 125 126 private boolean cleanup (File f) { 127 String chNames[] = f.list (); 128 boolean deleted = true; 129 for (int i=0; i < chNames.length; i++) { 130 File ch = new File (f.getAbsolutePath (), chNames [i]); 131 if (ch.isDirectory ()) { 132 deleted = deleted && cleanup (ch); 133 } else { 134 deleted = deleted && ch.delete (); 135 } 136 } 137 deleted = deleted && f.delete (); 138 return deleted; 139 } 140 141 private void _distribute(Iterator rootedEntries, File dir, String childModuleUri) { 142 FileLock lock = null; 143 InputStream in = null; 144 OutputStream out = null; 145 146 try { 147 if (! dir.exists()) 148 dir.mkdirs(); 149 150 File parent = dir.getParentFile(); 151 FileObject destRoot = FileUtil.toFileObject(parent); 152 153 FileObject[] garbages = destRoot.getChildren(); 154 for (int i=0; i<garbages.length; i++) { 155 garbages[i].delete(); 156 } 157 158 while(rootedEntries.hasNext()) { 159 J2eeModule.RootedEntry entry = (J2eeModule.RootedEntry) rootedEntries.next(); 160 String relativePath = entry.getRelativePath(); 161 FileObject sourceFO = entry.getFileObject(); 162 FileObject destFolder = ServerFileDistributor.findOrCreateParentFolder(destRoot, relativePath); 163 if (sourceFO.isData ()) { 164 FileUtil.copyFile(sourceFO, destFolder, sourceFO.getName()); 166 175 } 176 } 177 178 DeploymentConfigurationProvider dcp = dtarget.getDeploymentConfigurationProvider(); 180 DeploymentConfiguration config = dcp.getDeploymentConfiguration(); 181 182 DeployableObject deployable = dcp.getDeployableObject(childModuleUri); 184 185 } catch (Exception e) { 186 String msg = NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_IncrementalDeployFailed", e); 187 setStatusDistributeFailed(msg); 188 throw new RuntimeException (e); 189 } finally { 190 if (lock != null) { 191 try { lock.releaseLock(); } catch(Exception ex) {} 192 } 193 } 194 } 195 196 private void setStatusDistributeRunning(String message) { 198 notify(createRunningProgressEvent(CommandType.DISTRIBUTE, message)); 199 } 200 private void setStatusDistributeFailed(String message) { 201 notify(createFailedProgressEvent(CommandType.DISTRIBUTE, message)); 202 } 203 private void setStatusDistributeCompleted(String message) { 204 notify(createCompletedProgressEvent(CommandType.DISTRIBUTE, message)); 205 } 206 207 } 208 | Popular Tags |