001 /*
002 * Copyright 2006 Stephen J. McConnell.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.lang;
020
021 import java.io.IOException;
022 import java.net.URL;
023
024 /**
025 * Simple resource deployment strategy.
026 * @author <a href="http://www.dpml.net">Digital Product Management Library</a>
027 * @version 2.1.1
028 */
029 public class AntlibStrategy extends Strategy
030 {
031 private final String m_urn;
032 private final String m_path;
033
034 /**
035 * Creation of resource datatype.
036 * @param classloader the classloader
037 * @param urn the resource urn
038 * @param path the resource path
039 * @exception IOException if an I/O error occurs
040 */
041 public AntlibStrategy( ClassLoader classloader, String urn, String path )
042 throws IOException
043 {
044 super( classloader );
045 if( null == urn )
046 {
047 throw new NullPointerException( "urn" );
048 }
049 if( null == path )
050 {
051 throw new NullPointerException( "path" );
052 }
053 m_urn = urn;
054 m_path = path;
055 }
056
057 /**
058 * Return the strategy name.
059 * @return the name
060 */
061 public String getName()
062 {
063 return m_urn;
064 }
065
066 /**
067 * Return the strategy priority.
068 * @return the priority value
069 */
070 public int getPriority()
071 {
072 return 0;
073 }
074
075 /**
076 * Return the part content or null if the result type is unresolvable
077 * relative to the supplied classes argument. Class arguments recognized
078 * over an above plugin include the URL and String classes. If the URL
079 * class is supplied a URL referencing the resource identified by path
080 * is returned. If a String is requested the urn value is returned.
081 *
082 * @param c the content class
083 * @return the content
084 * @exception IOException if an IO error occurs
085 */
086 public <T>T getContentForClass( Class<T> c ) throws IOException
087 {
088 if( Strategy.class.equals( c ) )
089 {
090 return c.cast( this );
091 }
092 else if( URL.class.equals( c ) )
093 {
094 URL url = getClassLoader().getResource( m_path );
095 return c.cast( url );
096 }
097 else if( String.class.equals( c ) )
098 {
099 String urn = getURN();
100 return c.cast( urn );
101 }
102 else
103 {
104 return null;
105 }
106 }
107
108 /**
109 * Get the resource urn.
110 * @return the urn
111 */
112 public String getURN()
113 {
114 return m_urn;
115 }
116
117 /**
118 * Get the resource path.
119 * @return the path
120 */
121 public String getPath()
122 {
123 return m_path;
124 }
125
126 /**
127 * Return true if the strategy is a candidate for the supplied class.
128 * This calss will always throw a UnsupportedOperationException (anltib
129 * support is not applicable).
130 *
131 * @param type a service type
132 * @return the supported status flag
133 */
134 public boolean isaCandidate( Class<?> type )
135 {
136 throw new UnsupportedOperationException();
137 }
138
139 /**
140 * Strategy initialization (not used).
141 *
142 * @param registry a service registry
143 */
144 public void initialize( ServiceRegistry registry )
145 {
146 // not required
147 }
148
149 /**
150 * Instantiate a value.
151 * @param type the return type
152 * @return the resolved instance
153 */
154 public <T>T getInstance( Class<T> type )
155 {
156 try
157 {
158 ClassLoader classloader = getClassLoader();
159 Object resource = classloader.getResource( m_path );
160 return type.cast( resource );
161 }
162 catch( Exception e )
163 {
164 throw new PartError( e.getMessage(), e.getCause() );
165 }
166 }
167
168 /**
169 * Encode the resource strategy to XML.
170 * @param buffer the output buffer
171 * @param key the key
172 * @exception IOException if an I/O error occurs
173 */
174 public void encode( Buffer buffer, String key ) throws IOException
175 {
176 String urn = getURN();
177 String path = getPath();
178 buffer.nl( "<resource xmlns=\"" + AntlibStrategyHandler.NAMESPACE + "\"" );
179 if( null != key )
180 {
181 buffer.write( " key=\"" + key + "\"" );
182 }
183 buffer.write( " urn=\"" + urn + "\" " );
184 buffer.write( " path=\"" + path + "\"" );
185 buffer.write( "/>" );
186 }
187
188 /**
189 * Test if this instance is equal to the supplied instance.
190 * @param other the supplied instance
191 * @return the equality status
192 */
193 public boolean equals( Object other )
194 {
195 if( other instanceof AntlibStrategy )
196 {
197 AntlibStrategy resource = (AntlibStrategy) other;
198 if( !m_path.equals( resource.m_path ) )
199 {
200 return false;
201 }
202 else
203 {
204 return m_urn.equals( resource.m_urn );
205 }
206 }
207 else
208 {
209 return false;
210 }
211 }
212
213 /**
214 * Get the hashcode for this instance.
215 * @return the hash value
216 */
217 public int hashCode()
218 {
219 int hash = 99875845;
220 hash ^= m_path.hashCode();
221 hash ^= m_urn.hashCode();
222 return hash;
223 }
224 }