1 17 18 package org.apache.avalon.repository.meta; 19 20 import java.io.Serializable ; 21 import java.util.NoSuchElementException ; 22 import javax.naming.directory.Attributes ; 23 import javax.naming.directory.Attribute ; 24 import javax.naming.NamingException ; 25 26 import org.apache.avalon.repository.Artifact; 27 28 34 public class ArtifactDescriptor implements Serializable 35 { 36 40 public static final String DOMAIN_KEY = "meta.domain"; 41 public static final String VERSION_KEY = "meta.version"; 42 public static final String BUILD_KEY = 43 "avalon.artifact.signature"; 44 45 49 private final String c_domain; 50 private final String c_version; 51 52 private final String m_group; 53 private final String m_name; 54 private final String m_version; 55 private final String m_build; 56 57 private final Artifact m_artifact; 58 59 63 71 public ArtifactDescriptor( Attributes attributes ) 72 throws MetaException 73 { 74 if( null == attributes ) 75 throw new NullPointerException ( "attributes" ); 76 77 try 78 { 79 c_domain = getValue( attributes, DOMAIN_KEY ); 80 if( null == c_domain ) 81 { 82 final String error = 83 "Missing attribute: " + DOMAIN_KEY; 84 throw new MetaException( error ); 85 } 86 87 c_version = getValue( attributes, VERSION_KEY ); 88 if( null == c_version ) 89 { 90 final String error = 91 "Missing attribute: " + VERSION_KEY; 92 throw new MetaException( error ); 93 } 94 95 m_group = getAttribute( attributes, Artifact.GROUP_KEY, "" ); 96 m_name = getAttribute( attributes, Artifact.NAME_KEY, "" ); 97 m_version = getAttribute( attributes, Artifact.VERSION_KEY, "" ); 98 m_build = getAttribute( attributes, BUILD_KEY, "" ); 99 100 m_artifact = Artifact.createArtifact( m_group, m_name, m_version ); 101 102 } 103 catch( NamingException e ) 104 { 105 final String error = 106 "Unexpected naming exception during metadata creation."; 107 throw new MetaException( error, e ); 108 } 109 catch( NoSuchElementException e ) 110 { 111 final String error = 112 "Unexpected exception during metadata creation."; 113 throw new MetaException( error, e ); 114 } 115 } 116 117 121 125 public Artifact getArtifact() 126 { 127 return m_artifact; 128 } 129 130 134 public String getDomain() 135 { 136 return c_domain; 137 } 138 139 143 public String getVersion() 144 { 145 return c_version; 146 } 147 148 152 public String getBuild() 153 { 154 return m_build; 155 } 156 157 162 public boolean equals( Object other ) 163 { 164 boolean isEqual = other instanceof ArtifactDescriptor; 165 if ( isEqual ) 166 { 167 ArtifactDescriptor meta = (ArtifactDescriptor) other; 168 isEqual = isEqual && c_domain.equals( meta.c_domain ); 169 isEqual = isEqual && c_version.equals( meta.c_version ); 170 isEqual = isEqual && m_group.equals( meta.m_version ); 171 isEqual = isEqual && m_name.equals( meta.m_name ); 172 isEqual = isEqual && m_version.equals( meta.m_version ); 173 } 174 return isEqual; 175 } 176 177 181 public int hashCode() 182 { 183 int hash = 1; 184 hash >>>= 13; 185 hash ^= c_domain.hashCode(); 186 hash >>>= 13; 187 hash ^= c_version.hashCode(); 188 hash >>>= 13; 189 hash ^= m_group.hashCode(); 190 hash >>>= 13; 191 hash ^= m_version.hashCode(); 192 hash >>>= 13; 193 hash ^= m_build.hashCode(); 194 hash >>>= 13; 195 return hash; 196 } 197 198 202 public String toString() 203 { 204 return "[artifact: " + getDomain() 205 + "::" + getVersion() + "]"; 206 } 207 208 212 private String getAttribute( Attributes attributes, String key, String def ) 213 { 214 try 215 { 216 return getValue( attributes, key ); 217 } 218 catch( Throwable e ) 219 { 220 return def; 221 } 222 } 223 224 protected String getValue( Attributes attributes, String key ) 225 throws NamingException , NoSuchElementException 226 { 227 Attribute attribute = attributes.get( key ); 228 if( null == attribute ) return null; 229 Object object = attribute.get(); 230 if( null == object ) return null; 231 return object.toString(); 232 } 233 } 234 | Popular Tags |