KickJava   Java API By Example, From Geeks To Geeks.

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


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.config.*;
19 import org.jmanage.core.data.ApplicationConfigData;
20 import org.jmanage.core.data.MBeanData;
21 import org.jmanage.core.util.UserActivityLogger;
22 import org.jmanage.core.util.CoreUtils;
23 import org.jmanage.core.util.ACLConstants;
24 import org.jmanage.core.util.ErrorCodes;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  *
32  * date: Jan 17, 2005
33  * @author Rakesh Kalra
34  */

35 public class ConfigurationServiceImpl implements ConfigurationService {
36
37     public ApplicationConfigData addApplication(ServiceContext context,
38                                                 ApplicationConfigData data){
39
40         AccessController.checkAccess(context, ACLConstants.ACL_ADD_APPLICATIONS);
41         /* do the operation */
42         String JavaDoc appId = ApplicationConfig.getNextApplicationId();
43         Integer JavaDoc port = data.getPort();
44
45         ApplicationConfig config =
46                 ApplicationConfigFactory.create(appId, data.getName(),
47                         data.getType(),
48                         data.getHost(),
49                         port,
50                         data.getURL(),
51                         data.getUsername(),
52                         data.getPassword(),
53                         data.getParamValues());
54
55         try {
56             ApplicationConfigManager.addApplication(config);
57         } catch (ApplicationConfigManager.DuplicateApplicationNameException e) {
58             throw new ServiceException(ErrorCodes.APPLICATION_NAME_ALREADY_EXISTS,
59                     e.getAppName());
60         }
61
62         data.setApplicationId(appId);
63
64         /* log the operation */
65         UserActivityLogger.getInstance().logActivity(
66                 context.getUser().getUsername(),
67                 "Added application "+ "\""+config.getName()+"\"");
68
69         return data;
70     }
71
72     // TODO: sort the list before returning
73
public List JavaDoc getAllApplications(ServiceContext context) {
74         List JavaDoc appConfigs = ApplicationConfigManager.getApplications();
75         return appConfigListToAppConfigDataList(appConfigs);
76     }
77
78     private List JavaDoc appConfigListToAppConfigDataList(List JavaDoc appConfigs){
79         ArrayList JavaDoc appDataObjs = new ArrayList JavaDoc(appConfigs.size());
80         for(Iterator JavaDoc it=appConfigs.iterator(); it.hasNext(); ){
81             ApplicationConfigData configData = new ApplicationConfigData();
82             ApplicationConfig appConfig = (ApplicationConfig)it.next();
83             CoreUtils.copyProperties(configData, appConfig);
84             appDataObjs.add(configData);
85             if(appConfig.isCluster()){
86                 List JavaDoc childApplications =
87                         appConfigListToAppConfigDataList(appConfig.getApplications());
88                 configData.setChildApplications(childApplications);
89             }
90         }
91         return appDataObjs;
92     }
93
94
95     public List JavaDoc getConfiguredMBeans(ServiceContext context)
96             throws ServiceException {
97
98         AccessController.checkAccess(context,
99                 ACLConstants.ACL_VIEW_APPLICATIONS);
100
101         String JavaDoc applicationName = context.getApplicationConfig().getName();
102         ApplicationConfig appConfig =
103                 ServiceUtils.getApplicationConfigByName(applicationName);
104         List JavaDoc mbeanList = appConfig.getMBeans();
105         ArrayList JavaDoc mbeanDataList = new ArrayList JavaDoc(mbeanList.size());
106         for(Iterator JavaDoc it=mbeanList.iterator(); it.hasNext();){
107             MBeanConfig mbeanConfig = (MBeanConfig)it.next();
108             mbeanDataList.add(new MBeanData(mbeanConfig.getObjectName(),
109                     mbeanConfig.getName()));
110         }
111         return mbeanDataList;
112     }
113
114     public GraphConfig addGraph(ServiceContext context,GraphConfig graphConfig){
115         ApplicationConfig appConfig = context.getApplicationConfig();
116         //appConfig.addGraph(graphConfig);
117
try {
118             ApplicationConfigManager.updateApplication(appConfig);
119         } catch (ApplicationConfigManager.DuplicateApplicationNameException e) {
120             // unexpected exception
121
throw new RuntimeException JavaDoc(e);
122         }
123         return graphConfig;
124     }
125 }
126
Popular Tags