KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > ConfigStatus


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conf;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * Interface defines API to check the status of Cayenne configuration.
30  *
31  * @author Andrus Adamchik
32  */

33 public class ConfigStatus {
34
35     protected List JavaDoc otherFailures = new ArrayList JavaDoc();
36     protected Map JavaDoc failedMaps = new HashMap JavaDoc();
37     protected Map JavaDoc failedAdapters = new HashMap JavaDoc();
38     protected Map JavaDoc failedDataSources = new HashMap JavaDoc();
39     protected List JavaDoc failedMapRefs = new ArrayList JavaDoc();
40     protected Map JavaDoc messages = new HashMap JavaDoc();
41
42     public void addFailedMap(String JavaDoc name, String JavaDoc location, Object JavaDoc extraMessage) {
43         failedMaps.put(name, location);
44         if (extraMessage != null) {
45             messages.put(getMapMessageKey(name, location), extraMessage);
46         }
47     }
48
49     public void addFailedAdapter(String JavaDoc name, String JavaDoc location, String JavaDoc extraMessage) {
50         failedAdapters.put(name, location);
51         if (extraMessage != null) {
52             messages.put(getAdapterMessageKey(name, location), extraMessage);
53         }
54     }
55
56     public void addFailedDataSource(String JavaDoc name, String JavaDoc location, String JavaDoc extraMessage) {
57         failedDataSources.put(name, location);
58         if (extraMessage != null) {
59             messages.put(getDataSourceMessageKey(name, location), extraMessage);
60         }
61     }
62
63     public void addFailedMapRefs(String JavaDoc name, String JavaDoc extraMessage) {
64         failedMapRefs.add(name);
65         if (extraMessage != null) {
66             messages.put(getMapRefMessageKey(name), extraMessage);
67         }
68     }
69
70     protected String JavaDoc getMapMessageKey(String JavaDoc name, String JavaDoc location) {
71         return "map:" + name + ":" + location;
72     }
73
74     protected String JavaDoc getAdapterMessageKey(String JavaDoc name, String JavaDoc location) {
75         return "adapter:" + name + ":" + location;
76     }
77
78     protected String JavaDoc getDataSourceMessageKey(String JavaDoc name, String JavaDoc location) {
79         return "dataSource:" + name + ":" + location;
80     }
81
82     protected String JavaDoc getMapRefMessageKey(String JavaDoc name) {
83         return "map-ref:" + name;
84     }
85
86     /**
87      * Returns a String description of failed configuration pieces. Returns a canned "no
88      * failures" message if no failures occurred.
89      */

90     public String JavaDoc describeFailures() {
91         if (!hasFailures()) {
92             return "[No failures]";
93         }
94
95         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
96
97         Iterator JavaDoc it = failedMaps.keySet().iterator();
98         while (it.hasNext()) {
99             String JavaDoc name = (String JavaDoc) it.next();
100             String JavaDoc location = (String JavaDoc) failedMaps.get(name);
101             Object JavaDoc message = messages.get(getMapMessageKey(name, location));
102             buf.append("\n\tdomain.map.name=").append(name).append(
103                     ", domain.map.location=").append(location);
104             if (message != null) {
105                 buf.append(", reason: ").append(message);
106             }
107         }
108
109         it = failedAdapters.keySet().iterator();
110         while (it.hasNext()) {
111             String JavaDoc node = (String JavaDoc) it.next();
112             String JavaDoc adapter = (String JavaDoc) failedAdapters.get(node);
113             Object JavaDoc message = messages.get(getAdapterMessageKey(node, adapter));
114             buf.append("\n\tdomain.node.name=").append(node).append(
115                     ", domain.node.adapter=").append(adapter);
116             if (message != null) {
117                 buf.append(", reason: ").append(message);
118             }
119         }
120
121         it = failedDataSources.keySet().iterator();
122         while (it.hasNext()) {
123             String JavaDoc node = (String JavaDoc) it.next();
124             String JavaDoc location = (String JavaDoc) failedDataSources.get(node);
125             Object JavaDoc message = messages.get(getDataSourceMessageKey(node, location));
126             buf.append("\n\tdomain.node.name=").append(node).append(
127                     ", domain.node.datasource=").append(location);
128             if (message != null) {
129                 buf.append(", reason: ").append(message);
130             }
131         }
132
133         it = failedMapRefs.iterator();
134         while (it.hasNext()) {
135             String JavaDoc mapName = (String JavaDoc) it.next();
136             // don't report failed links if the DataMap itself failed to load
137
if (failedMaps.get(mapName) == null) {
138                 buf.append("\n\tdomain.node.map-ref.name=").append(mapName);
139
140                 Object JavaDoc message = messages.get(getMapRefMessageKey(mapName));
141                 if (message != null) {
142                     buf.append(", reason: ").append(message);
143                 }
144             }
145         }
146         return buf.toString();
147     }
148
149     /**
150      * Returns a list of error messages not directly associated with project objects, such
151      * as XML pare exceptions, IOExceptions, etc.
152      */

153     public List JavaDoc getOtherFailures() {
154         return otherFailures;
155     }
156
157     /**
158      * Returns a list of map reference names that failed to load.
159      */

160     public List JavaDoc getFailedMapRefs() {
161         return failedMapRefs;
162     }
163
164     /**
165      * Returns a map of locations for names of the data maps that failed to load.
166      */

167     public Map JavaDoc getFailedMaps() {
168         return failedMaps;
169     }
170
171     /**
172      * Returns a map of DataSource locations for node names that failed to load.
173      */

174     public Map JavaDoc getFailedDataSources() {
175         return failedDataSources;
176     }
177
178     /**
179      * Returns a map of adapter classes for node names that failed to load.
180      */

181     public Map JavaDoc getFailedAdapters() {
182         return failedAdapters;
183     }
184
185     /**
186      * Returns true if any of the "failed.." methods return true.
187      */

188     public boolean hasFailures() {
189         return failedMaps.size() > 0
190                 || failedDataSources.size() > 0
191                 || failedAdapters.size() > 0
192                 || failedMapRefs.size() > 0
193                 || otherFailures.size() > 0;
194     }
195 }
196
Popular Tags