001/* Module initializer for sensor-view 002 * 003 * Copyright (c) 2010 The Regents of the University of California. 004 * All rights reserved. 005 * 006 * '$Author: barseghian $' 007 * '$Date: 2010-09-01 17:42:22 -0700 (Wed, 01 Sep 2010) $' 008 * '$Revision: 25616 $' 009 * 010 * Permission is hereby granted, without written agreement and without 011 * license or royalty fees, to use, copy, modify, and distribute this 012 * software and its documentation for any purpose, provided that the above 013 * copyright notice and the following two paragraphs appear in all copies 014 * of this software. 015 * 016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 020 * SUCH DAMAGE. 021 * 022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 027 * ENHANCEMENTS, OR MODIFICATIONS. 028 * 029 */ 030 031package org.kepler.module.workflowschedulergui; 032 033import java.util.Iterator; 034import java.util.List; 035import java.util.Vector; 036 037import org.kepler.build.modules.Module; 038import org.kepler.configuration.ConfigurationManager; 039import org.kepler.configuration.ConfigurationProperty; 040import org.kepler.configuration.ConfigurationUtilities; 041import org.kepler.module.ModuleInitializer; 042import org.kepler.workflowscheduler.gui.WorkflowRunEngine; 043 044 045/** Module initializer for workflow-scheduler-gui. 046 * 047 * @author Daniel Crawl 048 * @version $Id: Initialize.java 25616 2010-09-02 00:42:22Z barseghian $ 049 */ 050public class Initialize implements ModuleInitializer 051{ 052 public static final String SCHEDULER = "scheduler"; 053 public static final String WORKFLOWRUNENGINELIST = "workflowRunEngineList"; 054 public static final String WORKFLOWRUNENGINE = "workflowRunEngine"; 055 public static final String NAME = "name"; 056 public static final String URL = "url"; 057 public static final String MODULENAME = "workflow-scheduler-gui"; 058 private static final String CURRENTMODULENAME = "workflow-scheduler-gui"; 059 private static final String GUI = "gui"; 060 private static final String PREFERENCESTABFACTORY = "preferencesTabFactory"; 061 private static final String TAB = "tab"; 062 private static ConfigurationProperty workflowSchedulerGUIProperty = null; 063 private static ConfigurationManager cm = ConfigurationManager.getInstance(); 064 private static String schedulerURL = null; 065 //private static String configFileLocation = null; 066 private static Vector<WorkflowRunEngine> workflowRunEngineList = new Vector<WorkflowRunEngine>(); 067 private static Module currentModule = null; 068 private static Module guiModule = null; 069 private static ConfigurationProperty guiModuleProperty = null; 070 071 /** Perform any module-specific initializations. */ 072 public void initializeModule() 073 { 074 //System.out.println("start of workflow-scheduler-gui module initializing"); 075 currentModule = ConfigurationManager.getModule(CURRENTMODULENAME); 076 ConfigurationUtilities.copyAllProperties("uiMenuMappings", "gui", "workflow-scheduler-gui"); 077 workflowSchedulerGUIProperty = cm.getProperty(currentModule); 078 schedulerURL = getSchedulerURLFromConfig(); 079 getWorkflowRunEngineListFromConfig(); 080 guiModule = ConfigurationManager.getModule(GUI); 081 guiModuleProperty = cm.getProperty(guiModule); 082 //addPeferenceTabToGUIConfig(); 083 overWritePeferenceTabOnGUIConfig(); 084 //copyConfigToPersistentDir(); 085 } 086 087 088 /** 089 * Gets the property of the current module (Sensor-view). 090 * @return the property 091 */ 092 public static ConfigurationProperty getCurrentModuleProperty() 093 { 094 if(workflowSchedulerGUIProperty != null) 095 { 096 return workflowSchedulerGUIProperty; 097 } 098 else 099 { 100 workflowSchedulerGUIProperty = cm.getProperty(ConfigurationManager.getModule(CURRENTMODULENAME)); 101 return workflowSchedulerGUIProperty; 102 } 103 104 } 105 106 /** 107 * Get the url of the scheduler. 108 * This method will read configuration file every time when it is called. 109 * @return the url of the scheduler 110 */ 111 public static String getSchedulerURL() 112 { 113 114 schedulerURL = getSchedulerURLFromConfig(); 115 return schedulerURL; 116 } 117 118 /** 119 * Get the list of workflow run engine. This method will read configuration file every time when it is called. 120 * @return the list of workfow run engine. 121 */ 122 public static Vector<WorkflowRunEngine> getWorkflowRunEngineList() 123 { 124 125 getWorkflowRunEngineListFromConfig(); 126 return workflowRunEngineList; 127 } 128 129 /** 130 * If user configured the schedulerURL on the configuration file. 131 * This method will read configuration file every time when it is called. 132 * @return true if schedulerURL was configured; false otherwise. 133 */ 134 public static boolean isSchedulerURLConfigured() 135 { 136 137 schedulerURL = getSchedulerURLFromConfig(); 138 return (schedulerURL != null && !schedulerURL.trim().equals("")); 139 } 140 141 142 /** 143 * If user configured the workflow run engine part on the configuration file. 144 * This method will read configuration file every time when it is called. 145 * @return true if use did configure the workflow run engine part; false otherwise. 146 */ 147 public static boolean isWorkflowRunEngineConfigured() 148 { 149 getWorkflowRunEngineListFromConfig(); 150 return (workflowRunEngineList.size() >= 1); 151 } 152 153 154 /** 155 * Get the path of the configuration file on KeplerData directory 156 * @return the path of the configuration file 157 */ 158 /*public static String getConfigFilePath() 159 { 160 return configFileLocation; 161 }*/ 162 163 /** 164 * Get the current module 165 * @return the current module 166 */ 167 public static Module getCurrentModule() 168 { 169 return currentModule; 170 } 171 172 /* 173 * Get scheduler url from configuration file 174 */ 175 private static String getSchedulerURLFromConfig() 176 { 177 String url = null; 178 ConfigurationProperty sensorViewConfiguration = getCurrentModuleProperty(); 179 if(sensorViewConfiguration != null) 180 { 181 ConfigurationProperty schedulerProperty = sensorViewConfiguration.getProperty(SCHEDULER); 182 if(schedulerProperty != null) 183 { 184 ConfigurationProperty urlProperty = schedulerProperty.getProperty(URL); 185 if(urlProperty != null) 186 { 187 url = urlProperty.getValue(); 188 } 189 190 } 191 192 } 193 return url; 194 } 195 196 /* 197 * Copy the configuration.xml of this module from application resource directory to kepler data 198 * driectory if the configuration.xml of this module doesn't exist on KeplerData directory 199 */ 200 /*private static void copyConfigToPersistentDir() 201 { 202 RootConfigurationProperty property = null; 203 List list = cm.getProperties(ConfigurationManager.getModule(CURRENTMODULENAME)); 204 if(list != null) 205 { 206 for (int i = 0; i < list.size(); i++) 207 { 208 RootConfigurationProperty rcp = (RootConfigurationProperty) list.get(i); 209 if (rcp.getNamespace().equals(ConfigurationProperty.namespaceDefault)) 210 { 211 property = rcp; 212 break; 213 } 214 } 215 } 216 if(property != null) 217 { 218 String filename = property.getFile().getName(); 219 Module m = property.getModule(); 220 221 //first check to see if the configurations directory exists in .kepler 222 File configDirOnResource = m.getConfigurationsDir(); 223 File configFileOnResource = new File(configDirOnResource, filename); 224 if(!configFileOnResource.exists()) 225 { 226 System.err.println("Intilaize.copyConfigToPersistentDir - the configure file "+filename+" doesn't exist on this directory "+configDirOnResource.getPath()); 227 return; 228 } 229 File modConfDir = DotKeplerManager.getInstance().getModuleConfigurationDirectory(m.getName()); 230 ///File modConfDir = DotKeplerManager.getInstance().getModuleConfigurationDirectory(m.getStemName()); 231 if(!modConfDir.exists()) 232 { //create the dir 233 modConfDir.mkdirs(); 234 } 235 //now we can save the property to keplerData/configurations if the configuration doesn't exist 236 try 237 { 238 File configFile = new File(modConfDir, filename); 239 configFileLocation = configFile.getAbsolutePath(); 240 if(!configFile.exists()) 241 { 242 //since serialize properties to kepler data directory will removing all comments on the configuration file, 243 //we will copy the file from resource directory to kepler data directory directly 244 FileReader reader = new FileReader(configFileOnResource); 245 FileWriter fw = new FileWriter(configFile); 246 char[] charArray = new char[1052]; 247 int size = reader.read(charArray); 248 while(size != -1) 249 { 250 fw.write(charArray, 0, size); 251 fw.flush(); 252 size = reader.read(charArray); 253 } 254 fw.close(); 255 reader.close(); 256 //System.out.println("=======copy the config file of the workflow-scheduler-gui from resource to kepler data"); 257 } 258 else 259 { 260 //System.out.println("=======NOT copy the config file of the workflow-scheduler-gui from resource to kepler data since the file exists"); 261 } 262 263 } 264 catch(Exception e) 265 { 266 System.err.println("Error copying configuration " + 267 filename + " to directory " + modConfDir.getAbsolutePath() + 268 " : " + e.getMessage()); 269 } 270 } 271 else 272 { 273 //System.out.println("=======NOT copy the config file of the workflow-scheduler-gui from resource to kepler data since the property is null"); 274 } 275 276 }*/ 277 278 279 /* 280 * Get the name list of the workflow run engines 281 */ 282 private static void getWorkflowRunEngineListFromConfig() 283 { 284 WorkflowRunEngine deletedEngine = null; 285 getWorkflowRunEngineListFromConfig(deletedEngine); 286 } 287 288 /** 289 * Remove the specified engine from configuration file. It only remove one even though 290 * the configuration file has duplicated ones. 291 * @param deletedEngine the engine will be removed 292 */ 293 public static void removeWorkflowRunEngineFromConfig(WorkflowRunEngine deletedEngine) 294 { 295 getWorkflowRunEngineListFromConfig(deletedEngine); 296 } 297 298 /* 299 * Get the name list of the workflow run engines. If deletedEngine is not null, 300 * the deletedEngine will be removed from configuration file as well. 301 */ 302 private static void getWorkflowRunEngineListFromConfig(WorkflowRunEngine deletedEngine) 303 { 304 boolean deleted = false; 305 //clear old result. 306 if(workflowRunEngineList != null) 307 { 308 workflowRunEngineList.clear(); 309 } 310 else 311 { 312 workflowRunEngineList = new Vector<WorkflowRunEngine>(); 313 } 314 315 ConfigurationProperty sensorViewConfiguration = Initialize.getCurrentModuleProperty(); 316 if(sensorViewConfiguration != null) 317 { 318 ConfigurationProperty workflowRunEngineListProperty = sensorViewConfiguration.getProperty(WORKFLOWRUNENGINELIST); 319 if(workflowRunEngineListProperty != null) 320 { 321 List<ConfigurationProperty> workflowRunEngineProperties =workflowRunEngineListProperty.getProperties(WORKFLOWRUNENGINE); 322 if(workflowRunEngineProperties != null) 323 { 324 Iterator<ConfigurationProperty> iterator = workflowRunEngineProperties.iterator(); 325 while(iterator.hasNext()) 326 { 327 ConfigurationProperty workflowRunEngine = iterator.next(); 328 if(workflowRunEngine != null) 329 { 330 ConfigurationProperty nameProperty = workflowRunEngine.getProperty(NAME); 331 ConfigurationProperty urlProperty = workflowRunEngine.getProperty(URL); 332 if(nameProperty != null && urlProperty != null && deletedEngine == null) 333 { 334 String name = nameProperty.getValue(); 335 //System.out.println("name is =========="+name); 336 String url = urlProperty.getValue(); 337 //System.out.println("url is =========="+url); 338 WorkflowRunEngine engine = new WorkflowRunEngine(name); 339 engine.setURL(url); 340 workflowRunEngineList.add(engine); 341 } 342 else if (nameProperty != null && urlProperty != null && deletedEngine != null) 343 { 344 String name = nameProperty.getValue(); 345 //System.out.println("name is =========="+name); 346 String url = urlProperty.getValue(); 347 //System.out.println("url is =========="+url); 348 if(name != null && name.equals(deletedEngine.getName()) && url != null && 349 url.equals(deletedEngine.getURL()) && !deleted) 350 { 351 //delete the configuration of this engine. 352 workflowRunEngineListProperty.removeProperty(workflowRunEngine); 353 deleted = true; 354 } 355 else 356 { 357 WorkflowRunEngine engine = new WorkflowRunEngine(name); 358 engine.setURL(url); 359 workflowRunEngineList.add(engine); 360 } 361 } 362 } 363 } 364 } 365 366 } 367 368 } 369 } 370 371 372 /* 373 * This method will overwrite the preference table on the GUI configuration.xml. 374 * addPeferenceTabToGUIConfig() is a better approach. However there is bug (5290) 375 * and we have to use overwriting approach 376 */ 377 private void overWritePeferenceTabOnGUIConfig() 378 { 379 ConfigurationProperty preferenceFactoryOnWorkflowSchedulerConfig = workflowSchedulerGUIProperty 380 .getProperty(PREFERENCESTABFACTORY); 381 ConfigurationProperty preferenceFactoryOnGUIConfig = guiModuleProperty 382 .getProperty(PREFERENCESTABFACTORY); 383 guiModuleProperty.overrideProperty(preferenceFactoryOnGUIConfig, preferenceFactoryOnWorkflowSchedulerConfig, true); 384 } 385 386 /* 387 * Add new preference tab configuration from workflow-scheduler-gui config 388 * to gui configuration. We need to make sure <originModule> element is added. 389 */ 390 private void addPeferenceTabToGUIConfig() 391 { 392 393 ConfigurationProperty preferenceFactoryWorkflowSchedulerGUI = workflowSchedulerGUIProperty 394 .getProperty(PREFERENCESTABFACTORY); 395 ConfigurationProperty preferenceFactoryGUI = guiModuleProperty 396 .getProperty(PREFERENCESTABFACTORY); 397 398 List<ConfigurationProperty> tabs = preferenceFactoryWorkflowSchedulerGUI 399 .getProperties(TAB); 400 for (ConfigurationProperty tab : tabs) { 401 try { 402 403 List<ConfigurationProperty> l = preferenceFactoryGUI.getProperties(); 404 boolean add = true; 405 for (int i = 0; i < l.size(); i++) { 406 ConfigurationProperty cp = (ConfigurationProperty) l.get(i); 407 String name = cp.getProperty("name").getValue(); 408 if (name.equals(tab.getProperty("name").getValue())) { 409 add = false; 410 } 411 } 412 413 if (add) { 414 preferenceFactoryGUI.addProperty(tab); 415 } 416 } catch (Exception e) { 417 e.printStackTrace(); 418 } 419 } 420 } 421 422}