KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.conf;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64 /**
65  * Interface defines API to check the status of Cayenne configuration.
66  *
67  * @author Andrei Adamchik
68  */

69 public class ConfigStatus {
70
71     protected List JavaDoc otherFailures = new ArrayList JavaDoc();
72     protected Map JavaDoc failedMaps = new HashMap JavaDoc();
73     protected Map JavaDoc failedAdapters = new HashMap JavaDoc();
74     protected Map JavaDoc failedDataSources = new HashMap JavaDoc();
75     protected List JavaDoc failedMapRefs = new ArrayList JavaDoc();
76     protected Map JavaDoc messages = new HashMap JavaDoc();
77
78     public void addFailedMap(String JavaDoc name, String JavaDoc location, Object JavaDoc extraMessage) {
79         failedMaps.put(name, location);
80         if(extraMessage != null) {
81             messages.put(getMapMessageKey(name, location), extraMessage);
82         }
83     }
84     
85     public void addFailedAdapter(String JavaDoc name, String JavaDoc location, String JavaDoc extraMessage) {
86         failedAdapters.put(name, location);
87         if(extraMessage != null) {
88             messages.put(getAdapterMessageKey(name, location), extraMessage);
89         }
90     }
91     
92     public void addFailedDataSource(String JavaDoc name, String JavaDoc location, String JavaDoc extraMessage) {
93         failedDataSources.put(name, location);
94         if(extraMessage != null) {
95             messages.put(getDataSourceMessageKey(name, location), extraMessage);
96         }
97     }
98     
99     public void addFailedMapRefs(String JavaDoc name, String JavaDoc extraMessage) {
100         failedMapRefs.add(name);
101         if(extraMessage != null) {
102             messages.put(getMapRefMessageKey(name), extraMessage);
103         }
104     }
105     
106     protected String JavaDoc getMapMessageKey(String JavaDoc name, String JavaDoc location) {
107         return "map:" + name + ":" + location;
108     }
109     
110     
111     protected String JavaDoc getAdapterMessageKey(String JavaDoc name, String JavaDoc location) {
112         return "adapter:" + name + ":" + location;
113     }
114     
115     protected String JavaDoc getDataSourceMessageKey(String JavaDoc name, String JavaDoc location) {
116         return "dataSource:" + name + ":" + location;
117     }
118     
119     protected String JavaDoc getMapRefMessageKey(String JavaDoc name) {
120         return "map-ref:" + name;
121     }
122     
123     /**
124      * Returns a String description of failed configuration pieces.
125      * Returns a canned "no failures" message if no failures occurred.
126      */

127     public String JavaDoc describeFailures() {
128         if(!hasFailures()) {
129             return "[No failures]";
130         }
131         
132         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
133         
134         Iterator JavaDoc it = failedMaps.keySet().iterator();
135         while(it.hasNext()) {
136             String JavaDoc name = (String JavaDoc)it.next();
137             String JavaDoc location = (String JavaDoc)failedMaps.get(name);
138             Object JavaDoc message = messages.get(getMapMessageKey(name, location));
139             buf.append("\n\tdomain.map.name=").append(name).append(", domain.map.location=").append(location);
140             if(message != null) {
141                 buf.append(", reason: ").append(message);
142             }
143         }
144  
145         it = failedAdapters.keySet().iterator();
146         while(it.hasNext()) {
147             String JavaDoc node = (String JavaDoc)it.next();
148             String JavaDoc adapter = (String JavaDoc)failedAdapters.get(node);
149             Object JavaDoc message = messages.get(getAdapterMessageKey(node, adapter));
150             buf.append("\n\tdomain.node.name=").append(node).append(", domain.node.adapter=").append(adapter);
151             if(message != null) {
152                 buf.append(", reason: ").append(message);
153             }
154         }
155         
156         it = failedDataSources.keySet().iterator();
157         while(it.hasNext()) {
158             String JavaDoc node = (String JavaDoc)it.next();
159             String JavaDoc location = (String JavaDoc)failedDataSources.get(node);
160             Object JavaDoc message = messages.get(getDataSourceMessageKey(node, location));
161             buf.append("\n\tdomain.node.name=").append(node).append(", domain.node.datasource=").append(location);
162             if(message != null) {
163                 buf.append(", reason: ").append(message);
164             }
165         }
166         
167         it = failedMapRefs.iterator();
168         while(it.hasNext()) {
169             String JavaDoc mapName = (String JavaDoc)it.next();
170             // don't report failed links if the DataMap itself failed to load
171
if(failedMaps.get(mapName) == null) {
172                 buf.append("\n\tdomain.node.map-ref.name=").append(mapName);
173                 
174                 Object JavaDoc message = messages.get(getMapRefMessageKey(mapName));
175                 if(message != null) {
176                     buf.append(", reason: ").append(message);
177                 }
178             }
179         }
180         return buf.toString();
181     }
182     
183     /**
184      * Returns a list of error messages not directly associated with project
185      * objects, such as XML pare exceptions, IOExceptions, etc.
186      */

187     public List JavaDoc getOtherFailures() {
188         return otherFailures;
189     }
190
191     /**
192      * Returns a list of map reference names that failed to load.
193      */

194     public List JavaDoc getFailedMapRefs() {
195         return failedMapRefs;
196     }
197
198     /**
199      * Returns a map of locations for names of the data maps that failed to
200      * load.
201      */

202     public Map JavaDoc getFailedMaps() {
203         return failedMaps;
204     }
205
206     /**
207      * Returns a map of DataSource locations for node names that failed to load.
208      */

209     public Map JavaDoc getFailedDataSources() {
210         return failedDataSources;
211     }
212
213     /**
214      * Returns a map of adapter classes for node names that failed to load.
215      */

216     public Map JavaDoc getFailedAdapters() {
217         return failedAdapters;
218     }
219
220     /**
221      * Returns true if any of the "failed.." methods return true.
222      */

223     public boolean hasFailures() {
224         return failedMaps.size() > 0
225             || failedDataSources.size() > 0
226             || failedAdapters.size() > 0
227             || failedMapRefs.size() > 0
228             || otherFailures.size() > 0;
229     }
230 }
231
Popular Tags