| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
package argos.deploy; |
| 32 | |
|
| 33 | |
import java.io.File; |
| 34 | |
import java.util.ArrayList; |
| 35 | |
import java.util.HashMap; |
| 36 | |
import java.util.List; |
| 37 | |
import java.util.Map; |
| 38 | |
import java.util.logging.Logger; |
| 39 | |
|
| 40 | |
import javax.management.MBeanNotificationInfo; |
| 41 | |
import javax.management.Notification; |
| 42 | |
|
| 43 | |
import argos.annotation.Description; |
| 44 | |
import argos.annotation.Instrument; |
| 45 | |
import argos.annotation.NotificationInfo; |
| 46 | |
import argos.annotation.NotificationSender; |
| 47 | |
import argos.annotation.RemoveInstrumentation; |
| 48 | |
import argos.config.Config; |
| 49 | |
import argos.metadata.ServiceMetaInfo; |
| 50 | |
import argos.naming.NamingService; |
| 51 | |
import argos.proxy.NotificationProxy; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
@RemoveInstrumentation |
| 59 | |
@Description("Loads new jar files (services)") |
| 60 | |
public class HotDeployer extends Thread { |
| 61 | 0 | private static final Logger logger = Logger.getLogger(HotDeployer.class.getName()); |
| 62 | 0 | private static HotDeployer singelton = new HotDeployer(); |
| 63 | |
|
| 64 | |
public static final String NEW_FILE_FOUND = "NEW_FILE_FOUND"; |
| 65 | |
public static final String DEPLOY_FILE_MISSING = "DEPLOY_FILE_MISSING"; |
| 66 | |
public static final String NOT_LOADING_FILE = "NOT_LOADING"; |
| 67 | |
public static final String FILE_UPDATED = "FILE_UPDATED"; |
| 68 | |
public static final String UNDEPLOY_FILE = "UNDEPLOY_FILE"; |
| 69 | |
|
| 70 | |
public static final String DEPLOY_FILE = "deploy.xml"; |
| 71 | |
public static final int DEFAULT_SLEEP_TIME = 1000; |
| 72 | |
|
| 73 | |
private String directory; |
| 74 | |
private int sleepTime; |
| 75 | |
private List<File> ignored; |
| 76 | |
private Map<String, Long> timestamps; |
| 77 | |
private boolean running; |
| 78 | |
|
| 79 | |
@NotificationSender |
| 80 | |
public NotificationProxy proxy; |
| 81 | |
private long seq; |
| 82 | |
|
| 83 | |
private HotDeployer() { |
| 84 | 0 | super("HotDeployer"); |
| 85 | 0 | seq = 0; |
| 86 | 0 | directory = Config.get(Config.DEFAULT_DEPLOY_FOLDER); |
| 87 | 0 | sleepTime = DEFAULT_SLEEP_TIME; |
| 88 | 0 | ignored = new ArrayList<File>(); |
| 89 | 0 | timestamps = new HashMap<String, Long>(); |
| 90 | 0 | running = true; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
public synchronized void stopDeployer() { |
| 94 | 0 | running = false; |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public void run() { |
| 99 | |
try { |
| 100 | 0 | sleep(1); |
| 101 | |
} |
| 102 | 0 | catch(InterruptedException ignore) {} |
| 103 | |
|
| 104 | 0 | ComponentManager cm = ComponentManager.getInstance(); |
| 105 | 0 | NamingService naming = NamingService.getInstance(); |
| 106 | |
|
| 107 | 0 | List<File> deploy = new ArrayList<File>(); |
| 108 | 0 | List<File> redeploy = new ArrayList<File>(); |
| 109 | 0 | List<ServiceMetaInfo> stop = new ArrayList<ServiceMetaInfo>(); |
| 110 | |
while(true) { |
| 111 | 0 | if(!running) { |
| 112 | 0 | return; |
| 113 | |
} |
| 114 | 0 | long start = System.currentTimeMillis(); |
| 115 | 0 | File dir = new File(directory); |
| 116 | 0 | if(dir.isDirectory()) { |
| 117 | 0 | for(File file : dir.listFiles()) { |
| 118 | 0 | if(file.getName().toLowerCase().endsWith(".jar")) { |
| 119 | 0 | if(isIgnored(file)) { |
| 120 | 0 | continue; |
| 121 | |
} |
| 122 | |
else { |
| 123 | |
|
| 124 | 0 | long filesize = file.length(); |
| 125 | |
while(true) { |
| 126 | |
try { |
| 127 | 0 | sleep(10); |
| 128 | |
} |
| 129 | 0 | catch (InterruptedException ignore) {} |
| 130 | 0 | if(!running) { |
| 131 | 0 | return; |
| 132 | |
} |
| 133 | 0 | long temp = file.length(); |
| 134 | 0 | if(filesize == temp) { |
| 135 | 0 | break; |
| 136 | |
} |
| 137 | |
else { |
| 138 | 0 | filesize = temp; |
| 139 | |
} |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
|
| 143 | 0 | if(!naming.isFileLoaded(file)) { |
| 144 | 0 | deploy.add(file); |
| 145 | |
} |
| 146 | 0 | else if(naming.isNewer(file)) { |
| 147 | 0 | redeploy.add(file); |
| 148 | |
} |
| 149 | |
} |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | stop.clear(); |
| 154 | 0 | for(File deployed : naming.getLoadedFiles()) { |
| 155 | 0 | boolean found = false; |
| 156 | 0 | for(File file : dir.listFiles()) { |
| 157 | 0 | if(deployed.equals(file)) { |
| 158 | 0 | found = true; |
| 159 | 0 | break; |
| 160 | |
} |
| 161 | |
} |
| 162 | 0 | if(!found) { |
| 163 | 0 | stop.add(naming.getServiceByFile(deployed)); |
| 164 | |
} |
| 165 | 0 | } |
| 166 | 0 | if(!stop.isEmpty()) { |
| 167 | 0 | cm.stop(stop); |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | 0 | boolean deployed = false; |
| 172 | 0 | if(!redeploy.isEmpty()) { |
| 173 | 0 | logger.info("Found updated file(s), starting redeployment."); |
| 174 | 0 | cm.redeploy(redeploy); |
| 175 | 0 | redeploy.clear(); |
| 176 | 0 | deployed = true; |
| 177 | |
} |
| 178 | 0 | if(!deploy.isEmpty()) { |
| 179 | 0 | logger.info("Found new file(s), starting deployment."); |
| 180 | 0 | cm.deploy(deploy); |
| 181 | 0 | deploy.clear(); |
| 182 | 0 | deployed = true; |
| 183 | |
} |
| 184 | 0 | if(deployed) { |
| 185 | 0 | double used = (System.currentTimeMillis() - start) / 1000.0; |
| 186 | 0 | logger.info("Deployment done in " + used + "s.\r\n"); |
| 187 | |
} |
| 188 | 0 | } |
| 189 | |
else { |
| 190 | 0 | logger.severe(dir.getName() + " is not a directory, unable to load components."); |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
try { |
| 195 | 0 | sleep(sleepTime); |
| 196 | |
} |
| 197 | 0 | catch(InterruptedException ignore) { |
| 198 | 0 | continue; |
| 199 | 0 | } |
| 200 | 0 | } |
| 201 | |
} |
| 202 | |
|
| 203 | |
public void addIgnore(File file) { |
| 204 | 0 | ignored.add(file); |
| 205 | 0 | timestamps.put(file.getName(), file.lastModified()); |
| 206 | 0 | proxy.send(new Notification(NOT_LOADING_FILE, Notification.class.getClass().getName(), seq++, file.getAbsolutePath())); |
| 207 | 0 | } |
| 208 | |
|
| 209 | |
public boolean removeIgnore(File file) { |
| 210 | 0 | for(File temp : ignored) { |
| 211 | 0 | if(temp.getName().equals(file.getName())) { |
| 212 | 0 | timestamps.remove(file.getName()); |
| 213 | 0 | return ignored.remove(temp); |
| 214 | |
} |
| 215 | |
} |
| 216 | 0 | return false; |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
private boolean isIgnored(File file) { |
| 221 | 0 | for(File temp : ignored) { |
| 222 | 0 | if(temp.getName().equals(file.getName())) { |
| 223 | 0 | long now = file.lastModified(); |
| 224 | 0 | long then = timestamps.get(temp.getName()); |
| 225 | 0 | if(now == then) { |
| 226 | 0 | return true; |
| 227 | |
} |
| 228 | |
else { |
| 229 | 0 | ignored.remove(temp); |
| 230 | 0 | logger.info(file.getName() + " has been updated, loading."); |
| 231 | 0 | return false; |
| 232 | |
} |
| 233 | |
} |
| 234 | |
} |
| 235 | 0 | return false; |
| 236 | |
} |
| 237 | |
|
| 238 | |
@Instrument |
| 239 | |
@Description("The current directory being monitored for new components") |
| 240 | |
public String getDirectory() { |
| 241 | 0 | return directory; |
| 242 | |
} |
| 243 | |
|
| 244 | |
@Instrument |
| 245 | |
public void setDirectory(String directory) { |
| 246 | 0 | this.directory = directory; |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
@Instrument |
| 250 | |
@Description("How many mili seconds between each scan for new components") |
| 251 | |
public int getSleepTime() { |
| 252 | 0 | return sleepTime; |
| 253 | |
} |
| 254 | |
|
| 255 | |
@Instrument |
| 256 | |
public void setSleepTime(int sleepTime) { |
| 257 | 0 | this.sleepTime = sleepTime; |
| 258 | 0 | } |
| 259 | |
|
| 260 | |
public static HotDeployer getInstance() { |
| 261 | 0 | return singelton; |
| 262 | |
} |
| 263 | |
|
| 264 | |
@NotificationInfo |
| 265 | |
public MBeanNotificationInfo[] getNotificationInfo() { |
| 266 | 0 | MBeanNotificationInfo[] not = new MBeanNotificationInfo[5]; |
| 267 | 0 | not[0] = new MBeanNotificationInfo(new String[]{NEW_FILE_FOUND}, Notification.class.getClass().getName(), "Emitted when a new file is found."); |
| 268 | 0 | not[1] = new MBeanNotificationInfo(new String[]{DEPLOY_FILE_MISSING}, Notification.class.getClass().getName(), "Emitted when the deploy file is missing from the jar."); |
| 269 | 0 | not[2] = new MBeanNotificationInfo(new String[]{NOT_LOADING_FILE}, Notification.class.getClass().getName(), "Emitted when a file is not being loaded."); |
| 270 | 0 | not[3] = new MBeanNotificationInfo(new String[]{FILE_UPDATED}, Notification.class.getClass().getName(), "Emitted when a file has been updated."); |
| 271 | 0 | not[4] = new MBeanNotificationInfo(new String[]{UNDEPLOY_FILE}, Notification.class.getClass().getName(), "Emitted when a is being undeployed."); |
| 272 | 0 | return not; |
| 273 | |
} |
| 274 | |
} |