KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > services > ServiceUtils


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  *
33  * date: Feb 21, 2005
34  * @author Rakesh Kalra
35  */

36 public class ServiceUtils {
37
38     private static final Logger JavaDoc logger = Loggers.getLogger(ServiceUtils.class);
39
40     // TODO: It will be nice to have a concept of ClusterServerConnection
41
// which will implement all cluster level operations - rk
42
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 JavaDoc 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 JavaDoc 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 JavaDoc resolveMBeanName(ApplicationConfig appConfig,
75                                           String JavaDoc mbeanName){
76         /* check if the mbeanName is the configured mbean name */
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 JavaDoc e) {
89                 logger.info("Error closing connection: " + e.getMessage());
90             }
91         }
92     }
93 }
94
Popular Tags