Skip to main content
summaryrefslogtreecommitdiffstats
blob: eeed594d9a0693708341a856cd0416b9430567eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.ote.core;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.eclipse.osee.framework.jdk.core.util.ChecksumUtil;

/**
 * @author Robert A. Fisher
 */
public class BundleInfo {
   private final String symbolicName;
   private final String version;
   private final URL systemLocation;
   private final URL bundleServerLocation;
   private final File file;
   private final Manifest manifest;
   private final boolean systemLibrary;
   private String md5Digest;

   public BundleInfo(URL systemLocation, String bundleServerBaseLocation, boolean systemLibrary) throws IOException {
      File tmpFile;
      try {
         tmpFile = new File(systemLocation.toURI());
      } catch (URISyntaxException ex) {
         tmpFile = new File(systemLocation.getPath());
      }
      this.file = tmpFile;

      JarFile jarFile = new JarFile(file);
      this.manifest = jarFile.getManifest();
      this.symbolicName = generateBundleName(manifest);
      this.version = manifest.getMainAttributes().getValue("Bundle-Version");

      this.systemLocation = systemLocation;
      this.bundleServerLocation = new URL(bundleServerBaseLocation + symbolicName);
      this.systemLibrary = systemLibrary;
      this.md5Digest = null;
   }

   public BundleInfo(URL systemLocation) throws IOException {
      File tmpFile;
      try {
         tmpFile = new File(systemLocation.toURI());
      } catch (URISyntaxException ex) {
         tmpFile = new File(systemLocation.getPath());
      }
      this.file = tmpFile;

      JarFile jarFile = new JarFile(file);
      this.manifest = jarFile.getManifest();
      this.symbolicName = generateBundleName(manifest);
      this.version = manifest.getMainAttributes().getValue("Bundle-Version");

      this.systemLocation = systemLocation;
      this.bundleServerLocation = systemLocation;
      this.systemLibrary = true;
      this.md5Digest = null;
   }

   /**
    * @return the name of the bundle
    */
   public static String generateBundleName(Manifest jarManifest) {
      String nameEntry = jarManifest.getMainAttributes().getValue("Bundle-SymbolicName");
	  if(nameEntry == null){
	     return "unknown";
	  }
      // Sometimes there's a semicolon then extra info - ignore this
      int index = nameEntry.indexOf(';');
      if (index != -1) {
         nameEntry = nameEntry.substring(0, index);
      }

      return nameEntry;
   }

   /**
    * @return the symbolicName
    */
   public String getSymbolicName() {
      return symbolicName;
   }

   /**
    * @return the version
    */
   public String getVersion() {
      return version;
   }

   /**
    * @return the location
    */
   public URL getSystemLocation() {
      return systemLocation;
   }

   /**
    * @return the bundleServerLocation
    */
   public URL getServerBundleLocation() {
      return bundleServerLocation;
   }

   /**
    * @return the file
    */
   public File getFile() {
      return file;
   }

   /**
    * @return the manifest
    */
   public Manifest getManifest() {
      return manifest;
   }

   @Override
   public String toString() {
      return getSymbolicName() + ":" + getVersion();
   }

   /**
    * @return the systemLibrary
    */
   public boolean isSystemLibrary() {
      return systemLibrary;
   }

   /**
    * @return the md5Digest
    */
   public String getMd5Digest() {
      // Do lazy calculation of this since it can be costly
      // and does not get read for all bundle info's
      if (md5Digest == null) {
         try {
            InputStream in = systemLocation.openStream();

            md5Digest = ChecksumUtil.createChecksumAsString(in, "MD5");

            in.close();
         } catch (NoSuchAlgorithmException ex) {
            throw new IllegalStateException("Always expect MD5 to be available", ex);
         } catch (IOException ex) {
            throw new IllegalStateException("Always expect local jar file to be available", ex);
         } catch (Exception e) {
			e.printStackTrace();
		}
      }
      return md5Digest;
   }
}

Back to the top