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.io.Writer; 023 import java.net.URL; 024 025 import net.dpml.util.Logger; 026 027 /** 028 * Resource part strategy implementation datatype. 029 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 030 * @version 1.0.3 031 */ 032 public class Resource extends Part 033 { 034 private final String m_urn; 035 private final String m_path; 036 037 /** 038 * Creation of resource datatype. 039 * @param logger the assigned logging channel 040 * @param info the part info descriptor 041 * @param classpath the part classpath descriptor 042 * @param urn the resource urn 043 * @param path the resource path 044 * @exception IOException if an I/O error occurs 045 */ 046 public Resource( Logger logger, Info info, Classpath classpath, String urn, String path ) 047 throws IOException 048 { 049 super( logger, info, classpath ); 050 if( null == urn ) 051 { 052 throw new NullPointerException( "urn" ); 053 } 054 if( null == path ) 055 { 056 throw new NullPointerException( "path" ); 057 } 058 m_urn = urn; 059 m_path = path; 060 } 061 062 /** 063 * Return the part content or null if the result type is unresolvable 064 * relative to the supplied classes argument. Class arguments recognized 065 * over an above plugin include the URL and String classes. If the URL 066 * class is supplied a URL referencing the resource identified by path 067 * is returned. If a String is requested the urn value is returned. 068 * 069 * @param c the content class 070 * @return the content 071 * @exception IOException if an IO error occurs 072 */ 073 protected Object getContent( Class c ) throws IOException 074 { 075 if( URL.class.equals( c ) ) 076 { 077 return getClassLoader().getResource( m_path ); 078 } 079 else if( String.class.equals( c ) ) 080 { 081 return getURN(); 082 } 083 else 084 { 085 return super.getContent( c ); 086 } 087 } 088 089 /** 090 * Get the resource urn. 091 * @return the urn 092 */ 093 public String getURN() 094 { 095 return m_urn; 096 } 097 098 /** 099 * Get the resource path. 100 * @return the path 101 */ 102 public String getPath() 103 { 104 return m_path; 105 } 106 107 /** 108 * Instantiate a value. 109 * @param args supplimentary arguments 110 * @return the resolved instance 111 * @exception Exception if a deployment error occurs 112 */ 113 public Object instantiate( Object[] args ) throws Exception 114 { 115 return getClassLoader().getResource( m_path ); 116 } 117 118 /** 119 * Encode the resource strategy to XML. 120 * @param writer the output stream writer 121 * @param pad the character offset 122 * @exception IOException if an I/O error occurs 123 */ 124 protected void encodeStrategy( Writer writer, String pad ) throws IOException 125 { 126 String urn = getURN(); 127 String path = getPath(); 128 writer.write( "\n <strategy xsi:type=\"resource\"" ); 129 writer.write( " urn=\"" + urn ); 130 writer.write( "\" path=\"" + path ); 131 writer.write( "\"/>" ); 132 } 133 134 /** 135 * Test if this instance is equal to the supplied instance. 136 * @param other the supplied instance 137 * @return the equality status 138 */ 139 public boolean equals( Object other ) 140 { 141 if( super.equals( other ) ) 142 { 143 if( other instanceof Resource ) 144 { 145 Resource resource = (Resource) other; 146 if( !m_path.equals( resource.m_path ) ) 147 { 148 return false; 149 } 150 else 151 { 152 return m_urn.equals( resource.m_urn ); 153 } 154 } 155 else 156 { 157 return false; 158 } 159 } 160 else 161 { 162 return false; 163 } 164 } 165 166 /** 167 * Get the hashcode for this instance. 168 * @return the hash value 169 */ 170 public int hashCode() 171 { 172 int hash = super.hashCode(); 173 hash ^= m_path.hashCode(); 174 hash ^= m_urn.hashCode(); 175 return hash; 176 } 177 }