Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1cd0dcd4715192fcb2d7e23dee3081865f98c523 (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
package org.eclipse.emf.cdo.spi.common.id;

import org.eclipse.emf.cdo.common.id.CDOIDLibraryDescriptor;
import org.eclipse.emf.cdo.common.id.CDOIDLibraryProvider;

import org.eclipse.net4j.util.io.ExtendedDataInput;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.om.OMBundle;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Eike Stepper
 * @since 2.0
 */
public class CDOIDLibraryProviderImpl implements CDOIDLibraryProvider
{
  private Map<String, OMBundle> bundles = new HashMap<String, OMBundle>();

  public CDOIDLibraryProviderImpl()
  {
  }

  public void addLibrary(String libraryName, OMBundle bundle)
  {
    bundles.put(libraryName, bundle);
  }

  public String[] getLibraryNames()
  {
    Set<String> libraryNames = bundles.keySet();
    return libraryNames.toArray(new String[libraryNames.size()]);
  }

  public CDOIDLibraryDescriptor createDescriptor(String factoryName)
  {
    return new Descriptor(factoryName);
  }

  public InputStream getContents(String libraryName) throws IOException
  {
    File library = getLibrary(libraryName);
    return IOUtil.openInputStream(library);
  }

  public int getSize(String libraryName)
  {
    File library = getLibrary(libraryName);
    return (int)library.length();
  }

  private File getLibrary(String libraryName)
  {
    OMBundle bundle = bundles.get(libraryName);
    if (bundle == null)
    {
      throw new IllegalStateException("Unknown library: " + libraryName);
    }

    URL url = bundle.getBaseURL();
    File file = new File(url.getFile());
    if (file.exists() && file.isDirectory())
    {
      file = new File(file, libraryName);
    }

    if (file.exists() && file.isFile() && file.getName().endsWith(".jar"))
    {
      return file;
    }

    throw new IllegalStateException("Not a JAR: " + file.getAbsolutePath());
  }

  /**
   * @author Eike Stepper
   */
  private final class Descriptor extends CDOIDLibraryDescriptorImpl
  {
    public Descriptor(String factoryName)
    {
      super(factoryName, CDOIDLibraryProviderImpl.this.getLibraryNames());
    }

    public Descriptor(ExtendedDataInput in) throws IOException
    {
      super(in);
    }
  }
}

Back to the top