KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > shark > SharkPublisher


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 13, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.plugin.shark;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.enhydra.shark.Shark;
30 import org.enhydra.shark.api.client.wfbase.BaseException;
31 import org.enhydra.shark.api.client.wfservice.PackageAdministration;
32 import org.enhydra.shark.api.client.wfservice.PackageHasActiveProcesses;
33 import org.enhydra.shark.api.client.wfservice.PackageInUse;
34 import org.enhydra.shark.api.client.wfservice.RepositoryMgr;
35 import org.pentaho.core.publisher.BasePublisher;
36 import org.pentaho.core.session.IPentahoSession;
37 import org.pentaho.core.system.PentahoSystem;
38 import org.pentaho.messages.Messages;
39
40 public class SharkPublisher extends BasePublisher {
41
42     /**
43      *
44      */

45     private static final long serialVersionUID = 3866069850517891673L;
46
47     private static final Log logger = LogFactory.getLog(SharkPublisher.class);
48
49     private HashMap JavaDoc modifiedXPDLs;
50
51     private HashMap JavaDoc unmodifiedXPDLs;
52
53     private HashMap JavaDoc newXPDLs;
54
55     private SharkManager shark = null;
56
57     public Log getLogger() {
58         return logger;
59     }
60
61     public String JavaDoc getName() {
62         return Messages.getString("SharkPublisher.USER_TITLE"); //$NON-NLS-1$
63
}
64
65     public String JavaDoc getDescription() {
66         return Messages.getString("SharkPublisher.USER_PUBLISH_DESCRIPTION"); //$NON-NLS-1$
67
}
68
69     public String JavaDoc publish(IPentahoSession session) {
70
71         try {
72             unmodifiedXPDLs = new HashMap JavaDoc();
73             modifiedXPDLs = new HashMap JavaDoc();
74             newXPDLs = new HashMap JavaDoc();
75
76             File JavaDoc solutionRoot = new File JavaDoc(PentahoSystem.getApplicationContext().getSolutionPath("")); //$NON-NLS-1$
77

78             shark = SharkManager.getInstance(session);
79             PackageAdministration packageAdmin = Shark.getInstance().getAdminInterface().getPackageAdministration();
80             RepositoryMgr repository = Shark.getInstance().getRepositoryManager();
81
82             // TODO: keep a list of the XPDL files in the solution so we can
83
// delete unwanted ones
84

85             // process the solution directories
86
if (debug)
87                 debug(Messages.getString("SharkPublisher.DEBUG_COPYING_FILES")); //$NON-NLS-1$
88
processDir(solutionRoot, repository, packageAdmin);
89
90             // delete any subdirectories and unused XPDLs
91
if (debug)
92                 debug(Messages.getString("SharkPublisher.DEBUG_CLEANSING_REPOSITORY")); //$NON-NLS-1$
93
cleanupExternalRepository();
94
95             // add any new XPDL files
96
if (debug)
97                 debug(Messages.getString("SharkPublisher.DEBUG_ADDING_NEW_FILES")); //$NON-NLS-1$
98
addXpdlFiles(newXPDLs, repository, packageAdmin);
99
100             // Adds any unmodified files that have not been updated AddXpldFiles
101
// will not update if they are already updated
102
if (debug)
103                 debug(Messages.getString("SharkPublisher.DEBUG_ADDING_UNMODIFIED_FILES")); //$NON-NLS-1$
104
addXpdlFiles(unmodifiedXPDLs, repository, packageAdmin);
105
106             // updated any modified XPDL files
107
if (debug)
108                 debug(Messages.getString("SharkPublisher.DEBUG_UPDATING_FILES")); //$NON-NLS-1$
109
updateXpdlFiles(modifiedXPDLs, repository, packageAdmin);
110
111             // This is a temporary call to do maintenance tasks after the
112
// publish
113
SharkMaintenance maintenance = new SharkMaintenance();
114             maintenance.doMaintenance(session);
115         } catch (Throwable JavaDoc t) {
116             error(Messages.getErrorString("SharkPublisher.ERROR_0009_PUBLISH_FAILED"), t); //$NON-NLS-1$
117
return Messages.getString("SharkPublisher.USER_ERROR_PUBLISH_FAILED") + t.getLocalizedMessage(); //$NON-NLS-1$
118
}
119         return Messages.getString("SharkPublisher.USER_WORKFLOW_UPDATED"); //$NON-NLS-1$
120

121     }
122
123     private void processDir(File JavaDoc root, RepositoryMgr repository, PackageAdministration packageAdmin) {
124
125         // process every file in this directory
126
File JavaDoc files[] = root.listFiles();
127         if (files != null) {
128             for (int i = 0; i < files.length; i++) {
129                 File JavaDoc file = files[i];
130                 if (file.getAbsolutePath().endsWith("shark\\repository\\external")) { //$NON-NLS-1$
131
// skip this
132
file = null;
133                 } else if (file.getAbsolutePath().endsWith("shark/repository/external")) { //$NON-NLS-1$
134
// skip this
135
file = null;
136                 } else if (file.isDirectory()) {
137                     // process this sub-directory
138
processDir(file, repository, packageAdmin);
139                 } else {
140                     if (file.getName().toLowerCase().endsWith(".xpdl")) { //$NON-NLS-1$
141
// process this XPDL file
142
copyXpdlFile(file);
143                     }
144                 }
145             }
146         }
147     }
148
149     private void copyXpdlFile(File JavaDoc xpdlFile) {
150
151         try {
152             // create a byte array to hold the bits and nibbles
153
long size = xpdlFile.length();
154             byte bytes[] = new byte[(int) size];
155             // read the file into the byte array
156
FileInputStream JavaDoc stream = new FileInputStream JavaDoc(xpdlFile);
157             stream.read(bytes);
158
159             // copy this file to the Shark external repository
160
File JavaDoc destination = new File JavaDoc(shark.getExternalRepositoryPath() + File.separator + xpdlFile.getName());
161             boolean modified = false;
162             if (destination.exists()) {
163                 long destinationSize = destination.length();
164                 if (size != destinationSize) {
165                     modified = true;
166                 } else {
167                     byte destinationBytes[] = new byte[(int) size];
168                     stream = new FileInputStream JavaDoc(destination);
169                     stream.read(destinationBytes);
170                     // TODO make this compare more intelligent
171
for (int i = 0; i < size; i++) {
172                         if (bytes[i] != destinationBytes[i]) {
173                             modified = true;
174                             break;
175                         }
176                     }
177                 }
178                 if (modified) {
179                     modifiedXPDLs.put(destination.getName(), ""); //$NON-NLS-1$
180
} else {
181                     unmodifiedXPDLs.put(destination.getName(), ""); //$NON-NLS-1$
182
}
183             } else {
184                 newXPDLs.put(destination.getName(), ""); //$NON-NLS-1$
185
modified = true;
186             }
187             if (modified) {
188                 modifiedXPDLs.put(destination.getName(), ""); //$NON-NLS-1$
189
FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(destination);
190                 outputStream.write(bytes);
191                 outputStream.flush();
192                 outputStream.close();
193             }
194         } catch (FileNotFoundException JavaDoc e) {
195             // this one is not very likey
196
error(Messages.getErrorString("SharkPublisher.ERROR_0001_COULD_NOT_COPY_MISSING_FILE", xpdlFile.getName())); //$NON-NLS-1$
197
e.printStackTrace();
198         } catch (IOException JavaDoc e) {
199             error(Messages.getErrorString("SharkPublisher.ERROR_0002_COULD_NOT_COPY_FILE", xpdlFile.getName())); //$NON-NLS-1$
200
e.printStackTrace();
201         }
202
203     }
204
205     private void cleanupExternalRepository() {
206
207         File JavaDoc externalRepositoryDirectory = new File JavaDoc(shark.getExternalRepositoryPath());
208         if (!externalRepositoryDirectory.exists()) {
209             error(Messages.getErrorString("SharkPublisher.ERROR_0003_EXTERNAL_REPOSITORY_DOES_NOT_EXIST")); //$NON-NLS-1$
210
return;
211         }
212
213         File JavaDoc files[] = externalRepositoryDirectory.listFiles();
214         HashMap JavaDoc packagesInUse = new HashMap JavaDoc();
215         if (files != null) {
216             for (int i = 0; i < files.length; i++) {
217                 File JavaDoc file = files[i];
218                 String JavaDoc fileName = file.getName();
219                 if (!fileName.toLowerCase().endsWith(".xpdl")) { //$NON-NLS-1$
220
// this is not an XPDL file, so delete it
221
deleteFile(file);
222                 } else {
223                     // check to see if this XPDL is still used
224

225                     if (!unmodifiedXPDLs.containsKey(fileName) && !modifiedXPDLs.containsKey(fileName) && !newXPDLs.containsKey(fileName)) {
226
227                         try {
228                             shark.unloadPackage(fileName);
229                             if (!shark.isPackageLoaded(fileName))
230                                 deleteFile(file);
231                         } catch (PackageHasActiveProcesses pe) {
232                             warn(Messages.getErrorString("SharkPublisher.ERROR_0004_UNLOAD_FAIL_PROCESSES_RUNNING", fileName)); //$NON-NLS-1$
233
} catch (PackageInUse pue) {
234                             warn(Messages.getErrorString("SharkPublisher.ERROR_0005_UNLOAD_FAILED_DUE_TO_REFERENCES", fileName)); //$NON-NLS-1$
235
packagesInUse.put(fileName, file);
236                         } catch (BaseException e) {
237                             error(Messages.getErrorString("SharkPublisher.ERROR_0006_UNLOAD_FAILED", fileName), e); //$NON-NLS-1$
238
}
239                     }
240                 }
241             }
242         }
243
244         // If the package was referenced by another external package, we assume
245
// at this point that the parent package has been deleted - try again to
246
// delete the child
247
for (Iterator JavaDoc it = packagesInUse.keySet().iterator(); it.hasNext();) {
248             String JavaDoc fileName = (String JavaDoc) it.next();
249             try {
250                 shark.unloadPackage(fileName);
251                 if (!shark.isPackageLoaded(fileName))
252                     deleteFile((File JavaDoc) packagesInUse.get(fileName));
253             } catch (Exception JavaDoc e) {
254                 error(Messages.getErrorString("SharkPublisher.ERROR_0006_UNLOAD_FAILED", fileName), e); //$NON-NLS-1$
255
}
256         }
257
258     }
259
260     private void deleteFile(File JavaDoc file) {
261         if (file.isDirectory()) {
262             File JavaDoc files[] = file.listFiles();
263             for (int i = 0; i < files.length; i++) {
264                 deleteFile(files[i]);
265             }
266         }
267
268         if (file.delete()) {
269             info(Messages.getString("SharkPublisher.INFO_DELETING_FILE", file.getAbsolutePath())); //$NON-NLS-1$
270
} else {
271             warn(Messages.getString("SharkPublisher.WARN_COULD_NOT_DELETE_FILE", file.getAbsolutePath())); //$NON-NLS-1$
272
}
273     }
274
275     private void addXpdlFiles(HashMap JavaDoc xpdlFileList, RepositoryMgr repository, PackageAdministration packageAdmin) {
276
277         Iterator JavaDoc fileListIterator = xpdlFileList.keySet().iterator();
278         while (fileListIterator.hasNext()) {
279             String JavaDoc xpdlFileName = (String JavaDoc) fileListIterator.next();
280             try {
281                 String JavaDoc pkgId = repository.getPackageId(xpdlFileName);
282                 if (!packageAdmin.isPackageOpened(pkgId)) {
283                     packageAdmin.openPackage(xpdlFileName);
284                 }
285             } catch (Exception JavaDoc e) {
286                 error(Messages.getErrorString("SharkPublisher.ERROR_0007_COULD_NOT_ADD_FILE", xpdlFileName)); //$NON-NLS-1$
287
e.printStackTrace();
288             }
289         }
290
291     }
292
293     private void updateXpdlFiles(HashMap JavaDoc xpdlFileList, RepositoryMgr repository, PackageAdministration packageAdmin) {
294
295         Iterator JavaDoc fileListIterator = xpdlFileList.keySet().iterator();
296         while (fileListIterator.hasNext()) {
297             String JavaDoc xpdlFileName = (String JavaDoc) fileListIterator.next();
298             try {
299                 String JavaDoc pkgId = repository.getPackageId(xpdlFileName);
300                 if (pkgId == null) {
301                     if (!packageAdmin.isPackageOpened(pkgId)) {
302                         packageAdmin.openPackage(xpdlFileName);
303                     }
304
305                 } else {
306                     packageAdmin.updatePackage(pkgId, xpdlFileName);
307                 }
308             } catch (Exception JavaDoc e) {
309                 error(Messages.getErrorString("SharkPublisher.ERROR_0008_COULD_NOT_UPDATE_FILE", xpdlFileName)); //$NON-NLS-1$
310
e.printStackTrace();
311             }
312         }
313
314     }
315 }
316
Popular Tags