001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.configuration;
027:
028: import com.sshtools.common.automate.*;
029:
030: import com.sshtools.j2ssh.configuration.*;
031:
032: import org.apache.commons.logging.*;
033:
034: import java.util.*;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.16 $
041: */
042: public class XmlConfigurationContext implements ConfigurationContext {
043: private static Log log = LogFactory
044: .getLog(XmlConfigurationContext.class);
045: HashMap configurations = new HashMap();
046: String apiResource = "sshtools.xml";
047: String automationResource = "automation.xml";
048: private boolean failOnError = false;
049:
050: /**
051: * Creates a new XmlConfigurationContext object.
052: */
053: public XmlConfigurationContext() {
054: }
055:
056: /**
057: *
058: *
059: * @param apiResource
060: */
061: public void setAPIConfigurationResource(String apiResource) {
062: this .apiResource = apiResource;
063: }
064:
065: /**
066: *
067: *
068: * @param automationResource
069: */
070: public void setAutomationConfigurationResource(
071: String automationResource) {
072: this .automationResource = automationResource;
073: }
074:
075: /**
076: *
077: *
078: * @param failOnError
079: */
080: public void setFailOnError(boolean failOnError) {
081: this .failOnError = failOnError;
082: }
083:
084: /**
085: *
086: *
087: * @throws ConfigurationException
088: */
089: public void initialize() throws ConfigurationException {
090: if (apiResource != null) {
091: try {
092: SshAPIConfiguration x = new SshAPIConfiguration(
093: ConfigurationLoader.loadFile(apiResource));
094: configurations
095: .put(
096: com.sshtools.j2ssh.configuration.SshAPIConfiguration.class,
097: x);
098: } catch (Exception ex) {
099: if (failOnError) {
100: throw new ConfigurationException(ex.getMessage());
101: } else {
102: log.info(apiResource + " could not be found: "
103: + ex.getMessage());
104: }
105: }
106: }
107:
108: if (automationResource != null) {
109: try {
110: AutomationConfiguration y = new AutomationConfiguration(
111: ConfigurationLoader
112: .loadFile(automationResource));
113: configurations
114: .put(
115: com.sshtools.common.automate.AutomationConfiguration.class,
116: y);
117: } catch (Exception ex) {
118: if (failOnError) {
119: throw new ConfigurationException(ex.getMessage());
120: } else {
121: log
122: .info(automationResource
123: + " could not be found: "
124: + ex.getMessage());
125: }
126: }
127: }
128: }
129:
130: /**
131: *
132: *
133: * @param cls
134: *
135: * @return
136: */
137: public boolean isConfigurationAvailable(Class cls) {
138: return configurations.containsKey(cls);
139: }
140:
141: /**
142: *
143: *
144: * @param cls
145: *
146: * @return
147: *
148: * @throws ConfigurationException
149: */
150: public Object getConfiguration(Class cls)
151: throws ConfigurationException {
152: if (configurations.containsKey(cls)) {
153: return configurations.get(cls);
154: } else {
155: throw new ConfigurationException(cls.getName()
156: + " configuration not available");
157: }
158: }
159: }
|