Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30148cfa52c9a3f8630f017846fd2467a4787990 (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
/*******************************************************************************
 * 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.runtimemanager;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.eclipse.osee.framework.ui.workspacebundleloader.IJarChangeListener;
import org.eclipse.osee.framework.ui.workspacebundleloader.JarCollectionNature;
import org.eclipse.osee.ote.core.BundleInfo;

/**
 * @author Robert A. Fisher
 */
public class JarListenerStub<T extends JarCollectionNature> implements IJarChangeListener<T> {

   private final Object bundleSynchronizer;
   private final Set<String> newBundles;
   private final Set<String> changedBundles;
   private final Set<String> removedBundles;

   public JarListenerStub() {
      this.bundleSynchronizer = new Object();
      this.newBundles = new HashSet<String>();
      this.changedBundles = new HashSet<String>();
      this.removedBundles = new HashSet<String>();
   }

   @Override
   public void handleBundleAdded(URL url) {
      try {
         String bundleName = getBundleNameFromJar(url);
         synchronized (bundleSynchronizer) {
            newBundles.add(bundleName);
            changedBundles.remove(bundleName);
            removedBundles.remove(bundleName);
         }
         System.out.println("Bundle added:" + bundleName);
      } catch (IOException ex) {
      }
   }

   @Override
   public void handleBundleChanged(URL url) {
      try {
         String bundleName = getBundleNameFromJar(url);
         synchronized (bundleSynchronizer) {
            changedBundles.add(bundleName);
            newBundles.remove(bundleName);
            removedBundles.remove(bundleName);
         }
         System.out.println("Bundle changed:" + bundleName);
      } catch (IOException ex) {
      }
   }

   @Override
   public void handleBundleRemoved(URL url) {
      try {
         String bundleName = getBundleNameFromJar(url);
         synchronized (bundleSynchronizer) {
            removedBundles.add(bundleName);
            newBundles.remove(bundleName);
            changedBundles.remove(bundleName);
         }
         System.out.println("Bundle removed:" + bundleName);
      } catch (IOException ex) {
      }
   }

   @Override
   public void handleNatureClosed(T nature) {
      System.out.println("Project closed: " + nature.getProject().getName());
      for (URL url : nature.getBundles()) {
         handleBundleRemoved(url);
      }
   }

   @Override
   public void handlePostChange() {
      System.out.println("Bunch of changes just finished");
   }

   private <S extends Object> Set<S> duplicateAndClear(Set<S> set) {
      synchronized (bundleSynchronizer) {
         Set<S> returnBundles = new HashSet<S>(set);
         set.clear();
         return returnBundles;
      }
   }

   /**
    * @return the newBundles
    */
   public Set<String> consumeNewBundles() {
      return duplicateAndClear(newBundles);
   }

   /**
    * @return the changedBundles
    */
   public Set<String> consumeChangedBundles() {
      return duplicateAndClear(changedBundles);
   }

   /**
    * @return the removedBundles
    */
   public Set<String> consumeRemovedBundles() {
      return duplicateAndClear(removedBundles);
   }

   private String getBundleNameFromJar(URL url) throws IOException {
      File file;
      try {
         file = new File(url.toURI());
      } catch (URISyntaxException ex) {
         file = new File(url.getPath());
      }

      JarFile jarFile = new JarFile(file);
      Manifest jarManifest = jarFile.getManifest();
      return BundleInfo.generateBundleName(jarManifest);
   }

}

Back to the top