final String[] locales = configuration.getStringArray("widget.locales");
final String contextPath = context.getContextPath();
Thread thr = new Thread(){
public void run() {
int interval = 5000;
WgtWatcher watcher = new WgtWatcher();
watcher.setWatchDir(deploy);
watcher.setListener(new WgtWatcher.FileChangeListener(){
public void fileModified(File f) {
// get persistence manager for this thread
IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
try{
persistenceManager.begin();
File upload = WidgetFileUtils.dealWithDroppedFile(UPLOADFOLDER, f);
W3CWidgetFactory fac = new W3CWidgetFactory();
fac.setLocales(locales);
fac.setLocalPath(contextPath+localWidgetFolderPath);
fac.setOutputDirectory(WIDGETFOLDER);
fac.setFeatures(Features.getFeatureNames());
fac.setStartPageProcessor(new StartPageProcessor());
W3CWidget model = fac.parse(upload);
WidgetJavascriptSyntaxAnalyzer jsa = new WidgetJavascriptSyntaxAnalyzer(fac.getUnzippedWidgetDirectory());
if(persistenceManager.findWidgetByGuid(model.getIdentifier()) == null) {
WidgetFactory.addNewWidget(model, null, upload, true);
String message = model.getLocalName("en") +"' - " + localizedMessages.getString("WidgetAdminServlet.19");
_logger.info(message);
FlashMessage.getInstance().message(message);
} else {
String message = model.getLocalName("en") +"' - " + localizedMessages.getString("WidgetAdminServlet.20");
WidgetFactory.update(model, persistenceManager.findWidgetByGuid(model.getIdentifier()), true, upload);
_logger.info(message);
FlashMessage.getInstance().message(message);
}
persistenceManager.commit();
} catch (IOException e) {
persistenceManager.rollback();
String error = f.getName()+":"+localizedMessages.getString("WidgetHotDeploy.1") + " - " + e.getLocalizedMessage();
FlashMessage.getInstance().error(error);
_logger.error(error, e);
} catch (BadWidgetZipFileException e) {
persistenceManager.rollback();
String error = f.getName()+":"+localizedMessages.getString("WidgetHotDeploy.2") + " - " + e.getLocalizedMessage();
FlashMessage.getInstance().error(error);
_logger.error(error, e);
} catch (BadManifestException e) {
persistenceManager.rollback();
String error = f.getName()+":"+localizedMessages.getString("WidgetHotDeploy.3") + " - " + e.getLocalizedMessage();
FlashMessage.getInstance().error(error);
_logger.error(error, e);
} catch (Exception e) {
persistenceManager.rollback();
String error = f.getName()+":"+e.getLocalizedMessage();
FlashMessage.getInstance().error(error);
_logger.error(error, e);
} finally {
// close thread persistence manager
PersistenceManagerFactory.closePersistenceManager();
}
}
public void fileRemoved(File f) {
// Not implemented - the .wgt files are removed as part of the deployment process
}
});
try {
while (true) {
watcher.check();
Thread.sleep(interval);
}
} catch (InterruptedException iex) {
}
}