001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.services;
016:
017: import org.jmanage.core.config.*;
018: import org.jmanage.core.data.ApplicationConfigData;
019: import org.jmanage.core.data.MBeanData;
020: import org.jmanage.core.util.UserActivityLogger;
021: import org.jmanage.core.util.CoreUtils;
022: import org.jmanage.core.util.ACLConstants;
023: import org.jmanage.core.util.ErrorCodes;
024:
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.Iterator;
028:
029: /**
030: *
031: * date: Jan 17, 2005
032: * @author Rakesh Kalra
033: */
034: public class ConfigurationServiceImpl implements ConfigurationService {
035:
036: public ApplicationConfigData addApplication(ServiceContext context,
037: ApplicationConfigData data) {
038:
039: AccessController.checkAccess(context,
040: ACLConstants.ACL_ADD_APPLICATIONS);
041: /* do the operation */
042: String appId = ApplicationConfig.getNextApplicationId();
043: Integer port = data.getPort();
044:
045: ApplicationConfig config = ApplicationConfigFactory.create(
046: appId, data.getName(), data.getType(), data.getHost(),
047: port, data.getURL(), data.getUsername(), data
048: .getPassword(), data.getParamValues());
049:
050: try {
051: ApplicationConfigManager.addApplication(config);
052: } catch (ApplicationConfigManager.DuplicateApplicationNameException e) {
053: throw new ServiceException(
054: ErrorCodes.APPLICATION_NAME_ALREADY_EXISTS, e
055: .getAppName());
056: }
057:
058: data.setApplicationId(appId);
059:
060: /* log the operation */
061: UserActivityLogger.getInstance().logActivity(
062: context.getUser().getUsername(),
063: "Added application " + "\"" + config.getName() + "\"");
064:
065: return data;
066: }
067:
068: // TODO: sort the list before returning
069: public List getAllApplications(ServiceContext context) {
070: List appConfigs = ApplicationConfigManager.getApplications();
071: return appConfigListToAppConfigDataList(appConfigs);
072: }
073:
074: private List appConfigListToAppConfigDataList(List appConfigs) {
075: ArrayList appDataObjs = new ArrayList(appConfigs.size());
076: for (Iterator it = appConfigs.iterator(); it.hasNext();) {
077: ApplicationConfigData configData = new ApplicationConfigData();
078: ApplicationConfig appConfig = (ApplicationConfig) it.next();
079: CoreUtils.copyProperties(configData, appConfig);
080: appDataObjs.add(configData);
081: if (appConfig.isCluster()) {
082: List childApplications = appConfigListToAppConfigDataList(appConfig
083: .getApplications());
084: configData.setChildApplications(childApplications);
085: }
086: }
087: return appDataObjs;
088: }
089:
090: public List getConfiguredMBeans(ServiceContext context)
091: throws ServiceException {
092:
093: AccessController.checkAccess(context,
094: ACLConstants.ACL_VIEW_APPLICATIONS);
095:
096: String applicationName = context.getApplicationConfig()
097: .getName();
098: ApplicationConfig appConfig = ServiceUtils
099: .getApplicationConfigByName(applicationName);
100: List mbeanList = appConfig.getMBeans();
101: ArrayList mbeanDataList = new ArrayList(mbeanList.size());
102: for (Iterator it = mbeanList.iterator(); it.hasNext();) {
103: MBeanConfig mbeanConfig = (MBeanConfig) it.next();
104: mbeanDataList
105: .add(new MBeanData(mbeanConfig.getObjectName(),
106: mbeanConfig.getName()));
107: }
108: return mbeanDataList;
109: }
110:
111: public GraphConfig addGraph(ServiceContext context,
112: GraphConfig graphConfig) {
113: ApplicationConfig appConfig = context.getApplicationConfig();
114: //appConfig.addGraph(graphConfig);
115: try {
116: ApplicationConfigManager.updateApplication(appConfig);
117: } catch (ApplicationConfigManager.DuplicateApplicationNameException e) {
118: // unexpected exception
119: throw new RuntimeException(e);
120: }
121: return graphConfig;
122: }
123: }
|