Coverage Report - argos.deploy.HotDeployer
 
Classes in this File Line Coverage Branch Coverage Complexity
HotDeployer
0%
0/108
0%
0/44
0
 
 1  
 /*
 2  
 Copyright (c) 2006, University of Tromsø
 3  
 All rights reserved.
 4  
 
 5  
 Redistribution and use in source and binary forms, with or without 
 6  
 modification, are permitted provided that the following conditions are met:
 7  
 
 8  
  * Redistributions of source code must retain the above copyright notice, this list 
 9  
    of conditions and the following disclaimer.
 10  
 
 11  
  * Redistributions in binary form must reproduce the above copyright notice, this 
 12  
    list of conditions and the following disclaimer in the documentation and/or other 
 13  
    materials provided with the distribution.
 14  
 
 15  
  * Neither the name of the University of Tromsø nor the names of its contributors may 
 16  
    be used to endorse or promote products derived from this software without specific 
 17  
    prior written permission.
 18  
 
 19  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 20  
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 21  
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
 22  
 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 23  
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 24  
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
 25  
 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 26  
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 27  
 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
 28  
 DAMAGE.
 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  
  * Created on 05.jul.2006
 55  
  * 
 56  
  * @author Dan Peder Eriksen
 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  
                                                         //Make sure the file isnt still being written to
 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  
                                                         //Deploy/redeploy
 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  
                                 //Check for missing files
 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  
                                 //Deploy
 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  
         //Returns true when a file is found in ignored and has not been updated.
 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  
 }