1 22 package org.jboss.ha.framework.server; 23 24 import java.io.File ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.*; 28 29 import org.jboss.deployment.Deployer; 30 import org.jboss.deployment.scanner.URLDeploymentScanner; 31 import org.jboss.ha.framework.interfaces.HAPartition; 32 import org.jboss.system.server.ServerConfig; 33 import org.jboss.system.server.ServerConfigLocator; 34 import org.jboss.system.server.ServerConfigUtil; 35 import org.jboss.ha.framework.server.ClusterFileTransfer.ClusterFileTransferException; 36 37 52 public class FarmMemberService extends URLDeploymentScanner implements FarmMemberServiceMBean 53 { 54 protected ClusterPartitionMBean mClusterPartition = null; 55 protected String mBackgroundPartition = ServerConfigUtil.getDefaultPartitionName(); 56 protected HAPartition mHAPartition; 57 private File mTempDirectory; 58 private ClusterFileTransfer mFileTransfer; 59 60 protected final static String SERVICE_NAME = "FarmMemberService"; 61 protected HashMap parentDUMap = new HashMap(); 62 63 protected ArrayList remotelyDeployed = new ArrayList (); 64 protected ArrayList remotelyUndeployed = new ArrayList (); 65 66 public String getPartitionName() 67 { 68 return mBackgroundPartition; 69 } 70 71 public void setPartitionName( String pPartitionName ) 72 { 73 log.warn("setPartitionName does nothing; use setClusterPartition"); 74 } 75 76 public ClusterPartitionMBean getClusterPartition() 77 { 78 return mClusterPartition; 79 } 80 81 public void setClusterPartition(ClusterPartitionMBean clusterPartition) 82 { 83 if( ( getState () != STARTED ) && ( getState () != STARTING ) ) 84 { 85 this.mClusterPartition = clusterPartition; 86 } 87 } 88 89 92 public void setFarmDeployDirectory(String urls) 93 throws MalformedURLException 94 { 95 super.setURLs(urls); 96 } 97 98 101 public void setScannerName(String name) 102 { 103 log.warn("ScannerName does nothing"); 104 } 105 106 108 public String getName() 109 { 110 return "Farm Member Service"; 111 } 112 113 117 protected void createService() throws Exception 118 { 119 super.createService(); 120 ServerConfig lConfig = ServerConfigLocator.locate(); 121 mTempDirectory = lConfig.getServerTempDir(); 122 123 createUnexistingLocalDir (); 124 } 125 129 protected void startService() 130 throws Exception 131 { 132 try 133 { 134 log.debug( "registerRPCHandler" ); 135 136 if (mClusterPartition == null) 137 { 138 throw new IllegalStateException ("Must set the ClusterPartition property before calling start"); 139 } 140 141 mHAPartition = mClusterPartition.getHAPartition(); 142 mBackgroundPartition = mHAPartition.getPartitionName(); 143 144 mHAPartition.registerRPCHandler( SERVICE_NAME, this ); 145 146 mFileTransfer = new ClusterFileTransfer(mHAPartition, buildParentFolderMapping()); 147 148 ArrayList response = mHAPartition.callMethodOnCoordinatorNode( 149 SERVICE_NAME, 150 "farmDeployments", 151 new Object [] {}, new Class [] {}, 152 true 153 ); 154 155 log.debug("Found "+response.size()+" farmDeployments responses"); 156 for (int i = 0; i < response.size(); i++) 157 { 158 Object map = response.get(i); 159 if ( map != null && map instanceof HashMap ) 160 { 161 HashMap farmed = (HashMap) map; 162 pullNewDeployments(mHAPartition, farmed); 163 } 164 } 165 166 scannerThread.doScan(); 168 169 scannerThread.setEnabled(isScanEnabled()); 171 } 172 catch(Exception e) 173 { 174 this.logException(e); 175 throw e; 176 } 177 } 178 179 180 protected void pullNewDeployments(HAPartition partition, HashMap farmed) 181 { 182 log.info("**** pullNewDeployments ****"); 183 Iterator it = farmed.keySet().iterator(); 184 while (it.hasNext()) 185 { 186 String depName = (String )it.next(); 187 DeployedURL du = (DeployedURL)parentDUMap.get(depName); 188 Date last = (Date)farmed.get(depName); 189 if (du != null) 190 { 191 Date theLast = new Date(du.getFile().lastModified()); 192 if (!theLast.before(last)) 193 { 194 continue; 195 } 196 } 197 198 String parentName = depName.substring(0, depName.indexOf('/')); 199 File destFile = new File (depName); 200 try 201 { 202 mFileTransfer.pull(destFile,parentName); 203 synchronized (remotelyDeployed) 204 { 205 remotelyDeployed.add (destFile.getName()); 206 } 207 } 208 catch(ClusterFileTransferException e) 209 { 210 this.logException(e); 212 } 213 } 214 } 215 216 private Map buildParentFolderMapping() 218 { 219 Map map = new HashMap(); 220 URL [] urls = (URL []) urlList.toArray( new URL [] {} ); 221 for (int i = 0; i < urlList.size(); i++) 222 { 223 if (urls[i].getProtocol().equals("file")) 224 { 225 File file = new File (urls[i].getFile()); 226 if (file.isDirectory()) 227 { 228 map.put(file.getName(),file); 229 } 230 231 } 232 } 233 return map; 234 } 235 236 protected File findParent(String parentName) 237 { 238 URL [] urls = (URL []) urlList.toArray( new URL [] {} ); 239 for (int i = 0; i < urlList.size(); i++) 240 { 241 if (urls[i].getProtocol().equals("file")) 242 { 243 File file = new File (urls[i].getFile()); 244 if (file.isDirectory()) 245 { 246 if (file.getName().equals(parentName)) return file; 247 } 248 } 249 } 250 return null; 251 } 252 253 public HashMap farmDeployments() 254 { 255 log.debug("farmDeployments request, parentDUMap.size="+parentDUMap.size()); 256 Iterator it = parentDUMap.keySet().iterator(); 257 HashMap farmed = new HashMap(); 258 while(it.hasNext()) 259 { 260 String key = (String )it.next(); 261 DeployedURL du = (DeployedURL)parentDUMap.get(key); 262 farmed.put(key, new Date(du.getFile().lastModified())); 263 } 264 return farmed; 265 } 266 267 public void farmDeploy( String parentName, File destFile, Date date ) 268 { 269 try 270 { 271 File parent = findParent(parentName); 272 if (parent == null) 273 { 274 log.info("Could not find parent: " + parentName + " for deployment: " + destFile + ", data: " + date); 275 return; 276 } 277 278 String fullName = parentName + "/" + destFile.getName(); 279 280 DeployedURL du = null; 281 synchronized(parentDUMap) 282 { 283 du = (DeployedURL)parentDUMap.get(fullName); 284 } 285 boolean deployIt = false; 286 if (du == null) 287 { 288 deployIt = true; 289 } 290 else 291 { 292 Date lastChanged = new Date(du.getFile().lastModified()); 293 deployIt = lastChanged.before(date); 294 } 295 296 if (deployIt) 297 { 298 synchronized (remotelyDeployed) 301 { 302 remotelyDeployed.add (fullName); 303 } 304 305 log.info( "farmDeployment(), deploy locally: " + fullName ); 306 File tempFile = new File (this.mTempDirectory, destFile.getName() ); 309 File lFarmFile = new File (parent,destFile.getName()); 310 if( lFarmFile.exists() ) { 311 if(!lFarmFile.delete()) { 312 log.info("could not delete target file for farm deployment "+ lFarmFile.getName()); 313 } 314 } 315 tempFile.setLastModified( date.getTime() ); 316 317 if(! ClusterFileTransfer.localMove(tempFile,lFarmFile )) 318 { 319 log.info("Could not move "+tempFile+" to " + lFarmFile); 320 } 321 } 322 else 323 { 324 log.info(fullName + " is already deployed by farm service on this node"); 325 } 326 } 327 catch( Exception e ) { 328 logException( e ); 329 } 330 } 331 332 public void farmUndeploy(String parentName, String fileName) 333 { 334 try { 335 log.info( "doUndeployment(), File: " + parentName + "/" + fileName); 337 File parent = findParent(parentName); 338 if (parent == null) 339 { 340 log.info("Could not find parent: " + parentName + " for undeployment: " + fileName); 341 return; 342 } 343 File deployed = new File (parent, fileName); 344 if (deployed.exists()) 345 { 346 synchronized (remotelyUndeployed) 349 { 350 String fullName = parentName + "/" + fileName; 351 remotelyUndeployed.add (fullName); 352 } 353 354 if(deployed.delete()) 355 log.info( "farmUndeployment(), removed file " + deployed ); 356 else 357 log.info( "farmUndeployment(), could not remove file " + deployed ); 358 } 359 } 360 catch( Exception e ) { 361 logException( e ); 362 } 363 } 364 365 protected void deploy(final DeployedURL du) 366 { 367 super.deploy(du); 368 File file = du.getFile(); 369 File parent = file.getParentFile(); 370 if (parent == null) return; 371 372 String fullName = parent.getName() + "/" + file.getName(); 373 synchronized (parentDUMap) 374 { 375 parentDUMap.put(fullName, du); 376 } 377 378 try 379 { 380 boolean consequenceOfRemoteCall = false; 384 synchronized (remotelyDeployed) 385 { 386 consequenceOfRemoteCall = remotelyDeployed.remove (fullName); 387 } 388 389 if (getState() == STARTING) return; 390 391 if (!consequenceOfRemoteCall) 392 { 393 Date fileDate = new Date(file.lastModified()); 394 395 this.mFileTransfer.push(file, parent.getName(), true); 396 397 mHAPartition.callMethodOnCluster( 398 SERVICE_NAME, 399 "farmDeploy", 400 new Object [] {parent.getName(), file, fileDate}, 401 new Class [] {String .class, File .class, Date.class}, 402 true 403 ); 404 } 405 } 406 catch (ClusterFileTransferException e) 407 { 408 logException(e); 409 } 410 catch (Exception ex) 411 { 412 logException(ex); 413 } 414 415 } 416 417 protected void undeploy(final DeployedURL du) 418 { 419 420 File file = du.getFile(); 421 File parent = file.getParentFile(); 422 String parentName = parent.getName(); 423 String fileName = file.getName(); 424 super.undeploy(du); 425 426 String fullName = parent.getName() + "/" + file.getName(); 427 synchronized (parentDUMap) 428 { 429 parentDUMap.remove(fullName); 430 } 431 432 if (getState() == STOPPING) return; 433 434 try 435 { 436 boolean consequenceOfRemoteCall = false; 440 synchronized (remotelyUndeployed) 441 { 442 consequenceOfRemoteCall = remotelyUndeployed.remove (fullName); 443 } 444 445 if (!consequenceOfRemoteCall) 446 { 447 mHAPartition.callMethodOnCluster( 448 SERVICE_NAME, 449 "farmUndeploy", 450 new Object [] {parentName, fileName}, 451 new Class [] {String .class, String .class}, 452 true 453 ); 454 } 455 } 456 catch (Exception ex) 457 { 458 logException(ex); 459 } 460 } 461 462 468 private void logException( Throwable e ) 469 { 470 if (e instanceof javax.management.RuntimeErrorException ) 471 { 472 e = ((javax.management.RuntimeErrorException )e).getTargetError(); 473 } 474 else if (e instanceof javax.management.RuntimeMBeanException ) 475 { 476 e = ((javax.management.RuntimeMBeanException )e).getTargetException(); 477 } 478 else if (e instanceof javax.management.RuntimeOperationsException ) 479 { 480 e = ((javax.management.RuntimeOperationsException )e).getTargetException(); 481 } 482 else if (e instanceof javax.management.MBeanException ) 483 { 484 e = ((javax.management.MBeanException )e).getTargetException(); 485 } 486 else if (e instanceof javax.management.ReflectionException ) 487 { 488 e = ((javax.management.ReflectionException )e).getTargetException(); 489 } 490 491 log.error(e); 492 } 493 494 protected void createUnexistingLocalDir() 495 { 496 if (this.urlList != null) 497 { 498 Iterator iter = this.urlList.iterator (); 499 while (iter.hasNext ()) 500 { 501 URL url = null; 502 try 503 { 504 url = (URL )iter.next (); 505 if (url.getProtocol().equals("file")) 506 { 507 File targetDir = new File (url.getFile ()); 508 if (!targetDir.exists ()) 509 targetDir.mkdirs (); 510 } 511 } 512 catch (Exception e) 513 { 514 log.info ("Problem while creating a farm directory: " + url, e); 515 } 516 } 517 } 518 } 519 } 520 | Popular Tags |