1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.daemon.configuration;
27
28 import com.sshtools.j2ssh.configuration.ConfigurationContext;
29 import com.sshtools.j2ssh.configuration.ConfigurationException;
30 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
31
32 import java.util.HashMap;
33
34
35 /**
36 *
37 *
38 * @author $author$
39 * @version $Revision: 1.13 $
40 */
41 public class XmlServerConfigurationContext implements ConfigurationContext {
42 HashMap configurations = new HashMap();
43 String serverResource = null;
44 String platformResource = null;
45 boolean failOnError = true;
46
47 /**
48 * Creates a new XmlServerConfigurationContext object.
49 */
50 public XmlServerConfigurationContext() {
51 }
52
53 /**
54 *
55 *
56 * @param serverResource
57 */
58 public void setServerConfigurationResource(String serverResource) {
59 this.serverResource = serverResource;
60 }
61
62 /**
63 *
64 *
65 * @param platformResource
66 */
67 public void setPlatformConfigurationResource(String platformResource) {
68 this.platformResource = platformResource;
69 }
70
71 /**
72 *
73 *
74 * @param failOnError
75 */
76 public void setFailOnError(boolean failOnError) {
77 this.failOnError = failOnError;
78 }
79
80 /**
81 *
82 *
83 * @throws ConfigurationException
84 */
85 public void initialize() throws ConfigurationException {
86 if (serverResource != null) {
87 try {
88 ServerConfiguration y = new ServerConfiguration(ConfigurationLoader.loadFile(
89 serverResource));
90 configurations.put(ServerConfiguration.class, y);
91 } catch (Exception ex) {
92 if (failOnError) {
93 throw new ConfigurationException(ex.getMessage());
94 }
95 }
96 }
97
98 if (platformResource != null) {
99 try {
100 PlatformConfiguration z = new PlatformConfiguration(ConfigurationLoader.loadFile(
101 platformResource));
102 configurations.put(PlatformConfiguration.class, z);
103 } catch (Exception ex) {
104 if (failOnError) {
105 throw new ConfigurationException(ex.getMessage());
106 }
107 }
108 }
109 }
110
111 /**
112 *
113 *
114 * @param cls
115 *
116 * @return
117 */
118 public boolean isConfigurationAvailable(Class cls) {
119 return configurations.containsKey(cls);
120 }
121
122 /**
123 *
124 *
125 * @param cls
126 *
127 * @return
128 *
129 * @throws ConfigurationException
130 */
131 public Object getConfiguration(Class cls) throws ConfigurationException {
132 if (configurations.containsKey(cls)) {
133 return configurations.get(cls);
134 } else {
135 throw new ConfigurationException(cls.getName() +
136 " configuration not available");
137 }
138 }
139 }