1 6 7 package com.quikj.application.web.talk.plugin; 8 9 import com.quikj.server.framework.*; 10 import com.quikj.server.web.*; 11 import com.quikj.client.raccess.*; 12 13 import java.util.*; 14 import java.sql.*; 15 16 20 public class FeatureFactory implements RemoteServiceInterface 21 { 22 private static FeatureFactory instance = null; 23 24 private String errorMessage = ""; 25 26 private HashMap featureList = new HashMap(); 28 private static HashMap instantiatedClassList = new HashMap(); 29 30 31 32 public FeatureFactory() 33 { 34 setInstance(this); 35 } 36 37 public boolean init() 38 { 39 41 Connection connection = JDBCConnection.getInstance().getNewConnection(TalkPluginApp.Instance().getDatabaseName()); 42 if (connection == null) 43 { 44 errorMessage = "FeatureFactory connection to the database server failed. " + JDBCConnection.getErrorMessage(); 45 return false; 46 } 47 48 FeatureTable tbl = new FeatureTable(); 49 tbl.setConnection(connection); 50 51 ArrayList list = tbl.queryAll(); 52 if (list == null) 53 { 54 errorMessage = "Error reading feature information from the database : " + tbl.getErrorMessage(); 55 try 56 { 57 connection.close(); 58 } 59 catch (SQLException ex) 60 { 61 } 62 63 return false; 64 } 65 66 for (Iterator i = list.iterator(); i.hasNext();) 67 { 68 FeatureTableElement data = (FeatureTableElement) i.next(); 69 70 if (data.isActive() == true) 71 { 72 FeatureElement feature = new FeatureElement(); 73 feature.setName(data.getName()); 74 feature.setClassName(data.getClassName()); 75 feature.setMap(data.getParams()); 76 77 featureList.put(new String (feature.getName()), feature); 78 } 79 } 80 81 try 82 { 83 connection.close(); 84 } 85 catch (SQLException ex) 86 { 87 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 88 "FeatureFactory.init() -- Error closing database connection : " + ex.getMessage()); 89 } 90 91 return true; 92 } 93 94 public void dispose() 95 { 96 AceRMIImpl rs = AceRMIImpl.getInstance(); 97 if (rs != null) { 99 rs.unregisterService("com.quikj.application.web.talk.plugin.FeatureFactory"); 100 } 101 102 setInstance(null); 103 } 104 105 public static FeatureFactory getInstance() 106 { 107 return instance; 108 } 109 110 private void setInstance(FeatureFactory instance) 111 { 112 FeatureFactory.instance = instance; 113 } 114 115 public int numFeatures() 116 { 117 return featureList.size(); 118 } 119 120 public String [] getFeatureNames() 121 { 122 String [] names = new String [featureList.size()]; 123 return ((String []) (featureList.keySet().toArray(names))); 124 } 125 126 private boolean startFeature(FeatureElement element) 127 { 128 Class app_class = (Class )instantiatedClassList.get(element.getClassName()); 130 131 if (app_class == null) { 133 try 134 { 135 app_class = Class.forName(element.getClassName()); 136 } 137 catch (ClassNotFoundException ex) 138 { 139 errorMessage = "ClassNotFoundException : " + ex.getMessage(); 140 return false; 141 } 142 143 Class [] interfaces = app_class.getInterfaces(); 145 146 int found = 0; 147 for (int i = 0; i < interfaces.length; i++) 148 { 149 if (interfaces[i].getName().equals("com.quikj.application.web.talk.plugin.FeatureInterface") 150 == true) 151 { 152 found++; 153 } 154 else if (interfaces[i].getName().equals("com.quikj.server.web.EndPointInterface") 155 == true) 156 { 157 found++; 158 } 159 } 160 161 if (found < 2) 162 { 163 errorMessage = "Class " + element.getClassName() 164 + " does not implement the necessary interfaces"; 165 return false; 166 } 167 168 instantiatedClassList.put(element.getClassName(), app_class); 169 170 } 171 FeatureInterface obj = null; 173 try 174 { 175 obj = (FeatureInterface)app_class.newInstance(); 176 } 177 catch (InstantiationException ex1) 178 { 179 errorMessage = "InstantiationException : " + ex1.getMessage(); 180 return false; 181 } 182 catch (IllegalAccessException ex2) 183 { 184 errorMessage = "IllegalAccessException : " + ex2.getMessage(); 185 return false; 186 } 187 188 if (obj.init(element.getName(), element.getMap()) == false) 189 { 190 errorMessage = "Class " + element.getClassName() 191 + " returned error during init"; 192 return false; 193 } 194 195 obj.start(); 197 element.setObj(obj); 198 199 return true; 200 } 201 202 public String getErrorMessage() 203 { 204 return errorMessage; 205 } 206 207 public boolean isFeature(String name) 208 { 209 return featureList.containsKey(name); 210 } 211 212 public void startUp() 213 { 214 216 AceRMIImpl rs = AceRMIImpl.getInstance(); 217 if (rs != null) { 219 rs.registerService("com.quikj.application.web.talk.plugin.FeatureFactory", 220 getInstance()); 221 } 222 223 225 Collection features = featureList.values(); 226 for (Iterator i = features.iterator(); i.hasNext();) 227 { 228 FeatureElement feature = (FeatureElement) i.next(); 229 try 230 { 231 if (startFeature(feature) == false) 232 { 233 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 234 "FeatureFactory.startUp() -- Feature " 235 + feature.getName() 236 + " not started - " 237 + getErrorMessage()); 238 } 239 else 240 { 241 AceLogger.Instance().log(AceLogger.INFORMATIONAL, 242 AceLogger.SYSTEM_LOG, 243 "Feature " 244 + feature.getName() 245 + " started"); 246 } 247 } 248 catch (Exception ex) 249 { 250 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 251 "FeatureFactory.startUp() -- " 252 + ex.getClass().getName() 253 + " occurred while creating feature: " 254 + feature.getName()); 255 } 256 } 257 } 258 259 public String getRMIParam(String param) 260 { 261 return null; 262 } 263 264 public boolean setRMIParam(String param, String value) 265 { 266 269 String str = "feature:"; 270 if (param.startsWith(str) == true) 271 { 272 String name = param.substring(str.length()); 273 if (name.length() > 0) 274 { 275 if (value.equals("activate") == true) 276 { 277 return activateFeature(name); 278 } 279 else if (value.equals("deactivate") == true) 280 { 281 return deactivateFeature(name); 282 } 283 else if (value.equals("synch") == true) 284 { 285 return synchFeature(name); 286 } 287 } 288 } 289 290 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 291 "FeatureFactory.setRMIParam() -- Invalid param and/or value received. Param = " 292 + param 293 + ", value = " 294 + value); 295 296 return false; 297 } 298 299 public synchronized boolean activateFeature(String name) 300 { 301 FeatureElement feature = getActiveFeatureInfo(name); 302 303 if (feature == null) 304 { 305 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 306 "FeatureFactory.activateFeature() -- Feature " 307 + name 308 + " not started - " 309 + getErrorMessage()); 310 311 return false; 312 } 313 314 try 315 { 316 318 FeatureElement old = (FeatureElement) featureList.get(name); 319 if (old != null) 320 { 321 featureList.remove(name); 322 323 FeatureInterface obj = old.getObj(); 324 if (obj != null) 325 { 326 obj.dispose(); 327 328 AceLogger.Instance().log(AceLogger.WARNING, 329 AceLogger.SYSTEM_LOG, 330 "Feature " 331 + name 332 + " being activated, but the feature was already running. Restarting the feature."); 333 } 334 } 335 336 338 featureList.put(new String (feature.getName()), feature); 339 340 if (startFeature(feature) == false) 341 { 342 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 343 "FeatureFactory.activateFeature() -- Feature " 344 + feature.getName() 345 + " not started - " 346 + getErrorMessage()); 347 348 featureList.remove(feature.getName()); 349 } 350 else 351 { 352 AceLogger.Instance().log(AceLogger.INFORMATIONAL, 353 AceLogger.SYSTEM_LOG, 354 "Feature " 355 + feature.getName() 356 + " started"); 357 358 return true; 359 } 360 } 361 catch (Exception ex) 362 { 363 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 364 "FeatureFactory.activateFeature() -- " 365 + ex.getClass().getName() 366 + " occurred while creating feature: " 367 + feature.getName()); 368 369 featureList.remove(feature.getName()); 370 } 371 372 return false; 373 } 374 375 public synchronized boolean deactivateFeature(String name) 376 { 377 FeatureElement feature = (FeatureElement) featureList.get(name); 378 if (feature != null) 379 { 380 featureList.remove(name); 381 382 FeatureInterface obj = feature.getObj(); 383 if (obj != null) 384 { 385 obj.dispose(); 386 387 AceLogger.Instance().log(AceLogger.INFORMATIONAL, 388 AceLogger.SYSTEM_LOG, 389 "Feature " 390 + feature.getName() 391 + " stopped"); 392 393 return true; 394 } 395 } 396 397 return true; 398 } 399 400 public synchronized boolean synchFeature(String name) 401 { 402 FeatureElement current_feature = (FeatureElement) featureList.get(name); 403 if (current_feature != null) 404 { 405 FeatureInterface obj = current_feature.getObj(); 406 if (obj != null) 407 { 408 FeatureElement new_feature_info = getActiveFeatureInfo(name); 410 411 if (new_feature_info == null) 412 { 413 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 414 "FeatureFactory.synchFeature() -- Feature " 415 + name 416 + " not updated - " 417 + getErrorMessage()); 418 419 return false; 420 } 421 422 obj.resynchParam(new_feature_info.getMap()); 423 current_feature.setMap(new_feature_info.getMap()); 424 425 AceLogger.Instance().log(AceLogger.INFORMATIONAL, 426 AceLogger.SYSTEM_LOG, 427 "Feature " 428 + name 429 + " parameters re-initialized"); 430 431 return true; 432 } 433 } 434 435 AceLogger.Instance().log(AceLogger.WARNING, 436 AceLogger.SYSTEM_LOG, 437 "Feature " 438 + name 439 + " being updated (synched), but the feature isn't running. Attempting to start the feature with the new data."); 440 441 return activateFeature(name); 442 } 443 444 private FeatureElement getActiveFeatureInfo(String name) 445 { 446 FeatureElement feature = null; 447 448 Connection connection = JDBCConnection.getInstance().getNewConnection(TalkPluginApp.Instance().getDatabaseName()); 449 if (connection == null) 450 { 451 errorMessage = "FeatureFactory connection to the database server failed. " + JDBCConnection.getErrorMessage(); 452 return null; 453 } 454 455 FeatureTable tbl = new FeatureTable(); 456 tbl.setConnection(connection); 457 458 FeatureTableElement data = tbl.query(name); 459 460 if (data == null) 461 { 462 if (tbl.getErrorMessage() == null) 463 { 464 errorMessage = "Feature name not found in database"; 465 } 466 else 467 { 468 errorMessage = tbl.getErrorMessage(); 469 } 470 } 471 else 472 { 473 if (data.isActive() == true) 474 { 475 feature = new FeatureElement(); 476 feature.setName(data.getName()); 477 feature.setClassName(data.getClassName()); 478 feature.setMap(data.getParams()); 479 } 480 else 481 { 482 errorMessage = "Feature information in database indicates feature not active"; 483 } 484 } 485 486 try 487 { 488 connection.close(); 489 } 490 catch (SQLException ex) 491 { 492 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG, 493 "FeatureFactory.getActiveFeatureInfo() -- Error closing database connection : " + ex.getMessage()); 494 } 495 496 return feature; 497 } 498 499 500 } 501 | Popular Tags |