1 20 21 package org.jahia.services.deamons.filewatcher.webappobserver; 22 23 import org.jahia.bin.Jahia; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.exceptions.JahiaInitializationException; 26 import org.jahia.registries.ServicesRegistry; 27 import org.jahia.security.license.License; 28 import org.jahia.services.deamons.filewatcher.JahiaFileWatcherService; 29 import org.jahia.services.jef.InvalidJefFileException; 30 import org.jahia.services.jef.JecFile; 31 import org.jahia.services.jef.JefFileInvalidKeyValueException; 32 import org.jahia.services.jef.JefFileKeyNotFoundException; 33 import org.jahia.services.sites.JahiaSite; 34 import org.jahia.services.sites.JahiaSitesService; 35 import org.jahia.services.webapps_deployer.JahiaWebAppsDeployerService; 36 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.util.Observable ; 40 import java.util.Observer ; 41 import java.util.Vector ; 42 43 49 public class WebAppsObserver implements Observer { 50 51 private static org.apache.log4j.Logger logger = 52 org.apache.log4j.Logger.getLogger (WebAppsObserver.class); 53 54 55 private boolean mUpdatedOnce = false; 56 57 58 private int mSiteID = -1; 59 60 61 private String mPath; 62 63 64 private boolean mCheckDate = false; 65 66 67 private long mInterval; 68 69 70 private boolean mFileOnly = true; 71 72 73 82 public WebAppsObserver (int siteID, 83 String path, 84 boolean checkDate, 85 long interval, 86 boolean fileOnly) 87 throws JahiaException { 88 89 mSiteID = siteID; 90 mPath = path; 91 mCheckDate = checkDate; 92 mInterval = interval; 93 mFileOnly = fileOnly; 94 95 JahiaFileWatcherService fws = ServicesRegistry 96 .getInstance () 97 .getJahiaFileWatcherService (); 98 99 if (fws != null) { 100 101 try { 102 fws.addFileWatcher (mPath, 103 mPath, 104 mCheckDate, 105 mInterval, 106 mFileOnly); 107 108 fws.registerObserver (mPath, this); 109 fws.startFileWatcher (mPath); 110 111 } catch (JahiaException e) { 112 113 logger.error ("init:: ", e); 114 throw new JahiaInitializationException ( 115 "WebAppsObserver::init failed ", e); 116 } 117 } 118 } 119 120 123 public void update (Observable subject, 124 Object args) { 125 126 synchronized (args) { 127 128 JahiaWebAppsDeployerService ads = ServicesRegistry.getInstance () 129 .getJahiaWebAppsDeployerService (); 130 131 JahiaSitesService ss = ServicesRegistry.getInstance () 132 .getJahiaSitesService (); 133 134 if (ads != null && ads.isInitialized () 135 && ss != null && ss.isInitialized () && Jahia.isInitiated ()) { 136 137 Vector files = (Vector ) args; 138 int size = files.size (); 139 File f = null; 140 JecFile jec = null; 141 boolean reScan = false; 142 143 License coreLicense = Jahia.getCoreLicense (); 144 145 if (coreLicense != null && !mUpdatedOnce) { 146 147 int jecLicenseType = 0; 149 int jecLicenseTypeRelComp = 0; 150 151 for (int i = 0; i < size; i++) { 153 f = (File) files.get (i); 154 if (f.isFile () && 155 (f.getName ().toLowerCase ()).endsWith (".jec")) { 156 157 try { 158 jec = new JecFile (f.getAbsolutePath ()); 159 jecLicenseType = jec.getLicenseType (); 160 jecLicenseTypeRelComp = jec. 161 getLicenseTypeRelComp (); 162 166 jec.extractFiles (); 167 f.delete (); 168 reScan = true; 169 172 jec = null; 173 } catch (JefFileKeyNotFoundException knf) { 174 File tmpFile = new File (f.getAbsolutePath () + 175 "_error"); 176 f.renameTo (tmpFile); 177 } catch (JefFileInvalidKeyValueException ikv) { 178 File tmpFile = new File (f.getAbsolutePath () + 179 "_error"); 180 f.renameTo (tmpFile); 181 } catch (InvalidJefFileException ijf) { 182 File tmpFile = new File (f.getAbsolutePath () + 183 "_error"); 184 f.renameTo (tmpFile); 185 } catch (IOException ioe) { 186 ioe.printStackTrace (); 187 } catch (JahiaException je) { 188 je.printStackTrace (); 190 } catch (Throwable t) { 191 t.printStackTrace (); 192 } 193 } 194 } 195 196 mUpdatedOnce = true; 197 } 198 199 if (reScan) { 200 org.jahia.services.database.ConnectionDispenser. 201 terminateConnection (); 202 return; 203 } 204 205 try { 206 JahiaSite site = ServicesRegistry.getInstance () 207 .getJahiaSitesService () 208 .getSite (mSiteID); 209 if (site != null) { 210 ads.deploy (site, files); 211 } 212 org.jahia.services.database.ConnectionDispenser. 213 terminateConnection (); 214 } catch (Throwable t) { 215 logger.error ("Web apps observer error : ", t); 216 org.jahia.services.database.ConnectionDispenser. 217 abortConnection (); 218 } 219 } 220 } 221 } 222 223 } | Popular Tags |