1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
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 implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.apache.juddi.v3.client;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.util.Enumeration;
21 import java.util.jar.Attributes;
22 import java.util.jar.JarFile;
23 import java.util.jar.Manifest;
24
25 /**
26 * The release class is a utility for obtaining the version number
27 */
28 public class Release {
29
30 private static final String UDDI_VERSION = "3.0";
31 private static final String JAR_NAME = "juddi-client";
32 private static String registryVersion = null;
33 public static final String UNKNOWN = "unknown";
34 private Release () {
35 }
36
37 /**
38 * Returns the version of this jUDDI-Client jar as defined in the manifest.
39 * Calls getRegistryVersion()
40 * @return the version number of the release
41 * @since 3.2
42 */
43 public static String getjUDDIClientVersion() {
44 return getRegistryVersion();
45 }
46 /**
47 * Returns the version of this jUDDI-Client jar as defined in the manifest.
48 * This function is poorly named, but left in place for backward compatibility
49 * @return the version number of the release
50 */
51 public static String getRegistryVersion() {
52 if (registryVersion == null) {
53 registryVersion = getVersionFromManifest(JAR_NAME);
54 }
55 return registryVersion;
56
57 }
58
59 public static String getUDDIVersion() {
60 return UDDI_VERSION;
61 }
62
63 public static String getVersionFromManifest(String jarName) {
64 Enumeration<URL> resEnum;
65 try {
66 resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
67 while (resEnum.hasMoreElements()) {
68 try {
69 URL url = (URL) resEnum.nextElement();
70 if (url.toString().toLowerCase().contains(jarName)) {
71 InputStream is = url.openStream();
72 if (is != null) {
73 Manifest manifest = new Manifest(is);
74 Attributes mainAttribs = manifest.getMainAttributes();
75 String version = mainAttribs.getValue("Bundle-Version");
76 if (version != null) {
77 return (version);
78 }
79 }
80 }
81 } catch (Exception e) {
82 // Silently ignore wrong manifests on classpath?
83 }
84 }
85 } catch (IOException e1) {
86 // Silently ignore wrong manifests on classpath?
87 }
88 return UNKNOWN;
89 }
90 }