1 16 package org.jmanage.core.services; 17 18 import org.jmanage.core.management.ServerConnection; 19 import org.jmanage.core.management.ServerConnector; 20 import org.jmanage.core.management.ConnectionFailedException; 21 import org.jmanage.core.config.ApplicationConfig; 22 import org.jmanage.core.config.ApplicationConfigManager; 23 import org.jmanage.core.config.MBeanConfig; 24 import org.jmanage.core.util.ErrorCodes; 25 import org.jmanage.core.util.Loggers; 26 27 import java.util.Iterator ; 28 import java.util.logging.Logger ; 29 import java.io.IOException ; 30 31 36 public class ServiceUtils { 37 38 private static final Logger logger = Loggers.getLogger(ServiceUtils.class); 39 40 public static ServerConnection getServerConnectionEvenIfCluster(ApplicationConfig appConfig){ 43 if(!appConfig.isCluster()){ 44 return ServerConnector.getServerConnection(appConfig); 45 }else{ 46 if(appConfig.getApplications().size() == 0){ 47 throw new ServiceException(ErrorCodes.CLUSTER_NO_APPLICATIONS); 48 } 49 for(Iterator it=appConfig.getApplications().iterator(); it.hasNext();){ 50 ApplicationConfig childAppConfig = (ApplicationConfig)it.next(); 51 try { 52 return ServerConnector.getServerConnection(childAppConfig); 53 } catch (ConnectionFailedException e) { 54 logger.info("Couldn't connect to childApp=" + 55 childAppConfig); 56 } 57 } 58 } 59 throw new ConnectionFailedException(null); 60 } 61 62 public static ApplicationConfig getApplicationConfigByName(String appName) 63 throws ServiceException { 64 65 ApplicationConfig appConfig = 66 ApplicationConfigManager.getApplicationConfigByName(appName); 67 if(appConfig == null){ 68 throw new ServiceException( 69 ErrorCodes.INVALID_APPLICATION_NAME, appName); 70 } 71 return appConfig; 72 } 73 74 public static String resolveMBeanName(ApplicationConfig appConfig, 75 String mbeanName){ 76 77 MBeanConfig mbeanConfig = appConfig.findMBean(mbeanName); 78 if(mbeanConfig != null){ 79 mbeanName = mbeanConfig.getObjectName(); 80 } 81 return mbeanName; 82 } 83 84 public static void close(ServerConnection connection){ 85 if(connection != null){ 86 try { 87 connection.close(); 88 } catch (IOException e) { 89 logger.info("Error closing connection: " + e.getMessage()); 90 } 91 } 92 } 93 } 94 | Popular Tags |