KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc;
29 import javax.enterprise.deploy.spi.Target JavaDoc;
30 import javax.enterprise.deploy.model.DeployableObject JavaDoc;
31 import org.openide.util.NbBundle;
32 import javax.enterprise.deploy.shared.CommandType JavaDoc;
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 /**
42  *
43  * @author nn136682
44  */

45 public class InitialServerFileDistributor extends ServerProgress {
46     ServerString serverString;
47     DeploymentTarget dtarget;
48     IncrementalDeployment incDeployment;
49     Target target;
50     boolean inPlace = false;
51
52     /** Creates a new instance of InitialServerFileDistributor */
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 JavaDoc deployable = deployment.getDeployableObject(null);
65         String JavaDoc 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 JavaDoc msg = NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_InPlaceNoSupport");
75                     setStatusDistributeFailed(msg);
76                     return null;
77                 } else {
78                     setStatusDistributeCompleted(NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_InPlaceDeployment", dir));//NOI18N
79
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 JavaDoc uri = childModules[i].getUrl();
92                     DeployableObject JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc relativePath = entry.getRelativePath();
161                 FileObject sourceFO = entry.getFileObject();
162                 FileObject destFolder = ServerFileDistributor.findOrCreateParentFolder(destRoot, relativePath);
163                 if (sourceFO.isData ()) {
164                     //try {
165
FileUtil.copyFile(sourceFO, destFolder, sourceFO.getName());
166                     /*} catch (java.io.SyncFailedException sfe) {
167                         in = sourceFO.getInputStream();
168                         FileObject destFO = destFolder.getFileObject(sourceFO.getName(), sourceFO.getExt());
169                         if (destFO != null) {
170                             lock = destFO.lock();
171                             out = destFO.getOutputStream(lock);
172                             FileUtil.copy(in, out);
173                         }
174                     }*/

175                 }
176             }
177             
178             // copying serverconfiguration files
179
DeploymentConfigurationProvider dcp = dtarget.getDeploymentConfigurationProvider();
180             DeploymentConfiguration JavaDoc config = dcp.getDeploymentConfiguration();
181
182             //Pending use childModuleUri for getDeploymentPlanFileNames
183
DeployableObject JavaDoc deployable = dcp.getDeployableObject(childModuleUri);
184             
185         } catch (Exception JavaDoc e) {
186             String JavaDoc msg = NbBundle.getMessage(InitialServerFileDistributor.class, "MSG_IncrementalDeployFailed", e);
187             setStatusDistributeFailed(msg);
188             throw new RuntimeException JavaDoc(e);
189         } finally {
190             if (lock != null) {
191                 try { lock.releaseLock(); } catch(Exception JavaDoc ex) {}
192             }
193         }
194     }
195     
196     //ServerProgress methods
197
private void setStatusDistributeRunning(String JavaDoc message) {
198         notify(createRunningProgressEvent(CommandType.DISTRIBUTE, message));
199     }
200     private void setStatusDistributeFailed(String JavaDoc message) {
201         notify(createFailedProgressEvent(CommandType.DISTRIBUTE, message));
202     }
203     private void setStatusDistributeCompleted(String JavaDoc message) {
204         notify(createCompletedProgressEvent(CommandType.DISTRIBUTE, message));
205     }
206
207 }
208
Popular Tags