Example usage for java.lang Package getPackage

List of usage examples for java.lang Package getPackage

Introduction

In this page you can find the example usage for java.lang Package getPackage.

Prototype

@CallerSensitive
@Deprecated(since = "9")
@SuppressWarnings("deprecation")
public static Package getPackage(String name) 

Source Link

Document

Finds a package by name in the caller's class loader and its ancestors.

Usage

From source file:org.apache.camel.util.PackageHelper.java

/**
 * Returns true if the version number of the given package name can be found and is greater than or equal to the minimum version.
 *
 * For package names which include multiple dots, the dots are removed. So for example a spring version of 2.5.1 is converted to
 * 2.51 so you can assert that its >= 2.51 (so above 2.50 and less than 2.52 etc).
 *
 * @param packageName the Java package name to compare
 * @param minimumVersion the minimum version number
 * @return true if the package name can be determined and if its greater than or equal to the minimum value
 *///from   w ww  .  j ava2s  .c  om
public static boolean isValidVersion(String packageName, double minimumVersion) {
    try {
        Package spring = Package.getPackage(packageName);
        if (spring != null) {
            String value = spring.getImplementationVersion();
            if (value != null) {
                // lets remove any extra dots in the string...
                int idx = value.indexOf('.');
                if (idx >= 0) {
                    StringBuilder buffer = new StringBuilder(value.substring(0, ++idx));
                    int i = idx;
                    for (int size = value.length(); i < size; i++) {
                        char ch = value.charAt(i);
                        if (Character.isDigit(ch)) {
                            buffer.append(ch);
                        }
                    }
                    value = buffer.toString();
                }

                if (ObjectHelper.isNotEmpty(value)) {
                    double number = Double.parseDouble(value);
                    return number >= minimumVersion;
                } else {
                    LOG.debug("Failed to find out version from package: " + packageName);
                }
            }
        }
    } catch (Exception e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Failed to find out version from package: " + packageName, e);
        }
    }

    return true;
}

From source file:com.link_intersystems.lang.reflect.Package2.java

public static Package2 get(String packageName) {
    Assert.notNull("packageName", packageName);
    return get(Package.getPackage(packageName));
}

From source file:PackageInfo.java

static void pkgInfo(ClassLoader classLoader, String pkgName, String className) {

    try {//from  w  w w  .  ja va 2s.c om
        classLoader.loadClass(pkgName + "." + className);

        Package p = Package.getPackage(pkgName);
        if (p == null) {
            System.out.println("WARNING: Package.getPackage(" + pkgName + ") is null");
        } else {
            System.out.println(p);
            System.out.println("Specification Title = " + p.getSpecificationTitle());
            System.out.println("Specification Vendor = " + p.getSpecificationVendor());
            System.out.println("Specification Version = " + p.getSpecificationVersion());

            System.out.println("Implementation Vendor = " + p.getImplementationVendor());
            System.out.println("Implementation Version = " + p.getImplementationVersion());
        }
    } catch (ClassNotFoundException e) {
        System.out.println("Unable to load " + pkgName);
    }

    System.out.println();
}

From source file:com.link_intersystems.lang.reflect.SerializablePackage.java

@Override
protected Package deserialize(Serializable restoreInfo) {
    String packageName = (String) restoreInfo;
    Package packageObject = Package.getPackage(packageName);
    if (packageObject == null) {
        throw new SerializationException("Unable to restore package " + packageName);
    }//w w w.  ja v  a  2 s  . co  m
    return packageObject;
}

From source file:net.bafeimao.umbrella.servers.world.test.MiscTests.java

@Test
public void test1() {
    Package pkg = Package.getPackage("net");
    Package.getPackages();
    System.out.println(pkg);
}

From source file:org.fusesource.mop.commands.Fork.java

/**
 * Forks a new child JVM and executes the remaining arguments as a child MOP
 * process/*  w ww  .ja  va2 s . c  o m*/
 */
@Command
public void fork(MOP mop, LinkedList<String> args) throws Exception {
    LOG.info("forking MOP with " + args);

    // TODO we could try find a mop jar on the URL class loader?

    Package aPackage = Package.getPackage("org.fusesource.mop");
    String version = aPackage.getImplementationVersion();
    if (version == null) {
        version = aPackage.getSpecificationVersion();
    }
    LOG.debug("mop package version: " + version);

    // TODO LATEST/RELEASE don't tend to work?
    /*
     * if (version == null) { version = "RELEASE"; }
     */
    String classpath;
    if (version != null) {
        ArtifactId mopArtifactId = mop.parseArtifactId("org.fusesource.mop:mop-core:" + version);
        mop.setTransitive(false);
        mop.setArtifactIds(Arrays.asList(mopArtifactId));
        classpath = mop.classpath();
    } else {
        classpath = System.getProperty("java.class.path");
        if (classpath == null || classpath.length() == 0) {
            throw new Exception("no java.class.path system property available!");
        }
    }
    if (isWindows() && classpath.contains(" ")) {
        classpath = "\"" + classpath + "\"";
    }

    List<String> newArgs = new ArrayList<String>();
    String javaExe = "java";
    if (isWindows()) {
        javaExe += ".exe";
    }
    newArgs.add(javaExe);

    //Propagate repository props to the forked process:
    for (Entry<String, String> entry : mop.getRepository().getRepositorySystemProps().entrySet()) {
        mop.setSystemProperty(entry.getKey(), entry.getValue());
    }

    mop.addSystemProperties(newArgs);
    newArgs.add("-D" + MOP.MOP_WORKING_DIR_SYSPROPERTY + "=" + mop.getWorkingDirectory().getAbsolutePath());

    newArgs.add("-cp");
    newArgs.add(classpath);
    newArgs.add(MOP.class.getName());
    newArgs.addAll(args);

    LOG.debug("About to execute: " + newArgs);
    mop.exec(newArgs);
}

From source file:monasca.log.api.MonApiApplication.java

private static void showVersion() {
    Package pkg;/*from www  .  j a v  a  2  s  . com*/
    pkg = Package.getPackage("monasca.log.api");

    System.out.println("-------- Version Information --------");
    System.out.println(pkg.getImplementationVersion());
}

From source file:org.jadira.scanner.classpath.types.JPackage.java

protected JPackage(String name, ClasspathResolver resolver) throws ClasspathAccessException {
    super(name, resolver);
    wrappedPackage = Package.getPackage(name);
}

From source file:de.bieniekconsulting.trafficmonitor.connector.snmp.ConnectorTestCase.java

/**
 * Define the deployment/*from  w w w  .  j  a v  a2s.co m*/
 *
 * @return The deployment archive
 */
@Deployment
public static ResourceAdapterArchive createDeployment() {
    ResourceAdapterArchive raa = ShrinkWrap.create(ResourceAdapterArchive.class, deploymentName + ".rar");

    JavaArchive ja = ShrinkWrap.create(JavaArchive.class, UUID.randomUUID().toString() + ".jar");
    ja.addPackages(true, Package.getPackage("de.bieniekconsulting.trafficmonitor.connector.snmp"));
    raa.addAsLibrary(ja);

    ja = ShrinkWrap.create(JavaArchive.class, UUID.randomUUID().toString() + ".jar");
    ja.addPackages(true, SystemInfo.class.getPackage());
    raa.addAsLibrary(ja);

    MavenResolverSystem resolver = Maven.resolver();

    raa.addAsLibraries(resolver.resolve("org.apache.commons:commons-lang3:3.3.2", "org.snmp4j:snmp4j:1.10.1")
            .withoutTransitivity().as(JavaArchive.class));

    raa.addAsManifestResource("META-INF/ironjacamar.xml", "ironjacamar.xml");

    return raa;
}

From source file:monasca.api.MonApiApplication.java

private static void showVersion() {
    Package pkg;/*w w w. ja va  2 s. c o m*/
    pkg = Package.getPackage("monasca.api");

    System.out.println("-------- Version Information --------");
    System.out.println(pkg.getImplementationVersion());
}