KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > logging > impl > ConfigurationParameter


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

17
18 package org.apache.avalon.logging.impl;
19
20 import java.io.File JavaDoc;
21 import java.net.URL JavaDoc;
22 import org.apache.avalon.util.criteria.Parameter;
23 import org.apache.avalon.util.criteria.CriteriaException;
24
25 import org.apache.avalon.util.i18n.ResourceManager;
26 import org.apache.avalon.util.i18n.Resources;
27
28 /**
29  * A parameter descriptor that supports transformation of a
30  * a string url to an URL instance.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version $Revision: 1.4 $
34  */

35 public class ConfigurationParameter extends Parameter
36 {
37     //--------------------------------------------------------------
38
// static
39
//--------------------------------------------------------------
40

41     private static final Resources REZ =
42       ResourceManager.getPackageResources( ConfigurationParameter.class );
43
44     //--------------------------------------------------------------
45
// constructors
46
//--------------------------------------------------------------
47

48    /**
49     * Transform a string to a string array.
50     * @param key the parameter key
51     */

52     public ConfigurationParameter( final String JavaDoc key )
53     {
54         super( key, URL JavaDoc.class );
55     }
56
57    /**
58     * Resolve a supplied string to a configuration
59     * @param value the value to resolve
60     * @exception CriteriaException if an error occurs
61     */

62     public Object JavaDoc resolve( Object JavaDoc value )
63       throws CriteriaException
64     {
65         if( value == null )
66             return null;
67         if( value instanceof URL JavaDoc )
68         {
69             return value;
70         }
71         if( value instanceof String JavaDoc )
72         {
73             return resolve( super.resolve( URL JavaDoc.class, value ) );
74         }
75         else if( value instanceof File JavaDoc )
76         {
77             File JavaDoc file = (File JavaDoc) value;
78             if( ! file.exists() )
79             {
80                 final String JavaDoc error =
81                   REZ.getString(
82                     "parameter.configuration.fnf.error",
83                     file.toString() );
84                 throw new CriteriaException( error );
85             }
86
87             try
88             {
89                 return file.toURL();
90             }
91             catch( Throwable JavaDoc e )
92             {
93                 final String JavaDoc error =
94                   REZ.getString(
95                     "parameter.configuration.file.error",
96                     file.toString() );
97                 throw new CriteriaException( error );
98             }
99         }
100         else
101         {
102             final String JavaDoc error =
103               REZ.getString(
104                 "parameter.unknown",
105                 value.getClass().getName(), URL JavaDoc.class.getName() );
106             throw new CriteriaException( error );
107         }
108     }
109 }
110
Popular Tags