<commit_before>/*
 * #%L
 * JBossOSGi Framework
 * %%
 * Copyright (C) 2010 - 2012 JBoss by Red Hat
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.jboss.osgi.framework.spi;

import static org.jboss.osgi.framework.FrameworkLogger.LOGGER;
import static org.jboss.osgi.framework.FrameworkMessages.MESSAGES;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.modules.Module;
import org.jboss.msc.service.ServiceContainer;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.osgi.framework.Services;
import org.jboss.osgi.framework.spi.LockManager.LockContext;
import org.jboss.osgi.framework.spi.LockManager.LockSupport;
import org.jboss.osgi.framework.spi.LockManager.Method;
import org.jboss.osgi.metadata.OSGiMetaData;
import org.jboss.osgi.resolver.XBundle;
import org.jboss.osgi.resolver.XBundleRevision;
import org.jboss.osgi.resolver.XEnvironment;
import org.jboss.osgi.resolver.spi.AbstractElement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
import org.osgi.framework.startlevel.BundleStartLevel;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleRevisions;
import org.osgi.framework.wiring.BundleWiring;

/**
 * An abstract implementation that adapts a {@link Module} to a {@link Bundle}
 *
 * @author <EMAIL>
 * @since 30-May-2012
 */
public class AbstractBundleAdaptor extends AbstractElement implements XBundle, LockManager.LockableItem {

    private final AtomicInteger bundleState = new AtomicInteger(Bundle.RESOLVED);
    private final LockSupport bundleLock = LockManager.Factory.addLockSupport(this);
    private final BundleManager bundleManager;
    private final BundleContext context;
    private final XBundleRevision brev;
    private final Module module;
    private BundleActivator bundleActivator;
    private long lastModified;

    public AbstractBundleAdaptor(BundleContext context, Module module, XBundleRevision brev) {
        if (context == null)
            throw MESSAGES.illegalArgumentNull("context");
        if (module == null)
            throw MESSAGES.illegalArgumentNull("module");
        if (brev == null)
            throw MESSAGES.illegalArgumentNull("brev");
        XBundle sysbundle = (XBundle) context.getBundle();
        this.bundleManager = sysbundle.adapt(BundleManager.class);
        this.context = context;
        this.module = module;
        this.brev = brev;

        this.lastModified = System.currentTimeMillis();
    }

    @Override
    public long getBundleId() {
        Long bundleId = brev.getAttachment(Long.class);
        return bundleId != null ? bundleId.longValue() : -1;
    }

    @Override
    public String getLocation() {
        String location = module.getIdentifier().getName();
        String slot = module.getIdentifier().getSlot();
        if (!slot.equals("main")) {
            location += ":" + slot;
        }
        return location;
    }

    @Override
    public String getSymbolicName() {
        return module.getIdentifier().getName();
    }

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

    @Override
    public int getState() {
        return bundleState.get();
    }

    @Override
    public Version getVersion() {
        String slot = module.getIdentifier().getSlot();
        try {
            return Version.parseVersion(slot);
        } catch (IllegalArgumentException ex) {
            return Version.emptyVersion;
        }
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return module.getClassLoader().loadClass(name);
    }

    /**
     * Required by spec:
     *<commit_msg>[AS7-6211] OSGi bundles show version 0.0.0 in AS7<commit_after>/*
 * #%L
 * JBossOSGi Framework
 * %%
 * Copyright (C) 2010 - 2012 JBoss by Red Hat
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */
package org.jboss.osgi.framework.spi;

import static org.jboss.osgi.framework.FrameworkLogger.LOGGER;
import static org.jboss.osgi.framework.FrameworkMessages.MESSAGES;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.modules.Module;
import org.jboss.msc.service.ServiceContainer;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.osgi.framework.Services;
import org.jboss.osgi.framework.spi.LockManager.LockContext;
import org.jboss.osgi.framework.spi.LockManager.LockSupport;
import org.jboss.osgi.framework.spi.LockManager.Method;
import org.jboss.osgi.metadata.OSGiMetaData;
import org.jboss.osgi.resolver.XBundle;
import org.jboss.osgi.resolver.XBundleRevision;
import org.jboss.osgi.resolver.XEnvironment;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.spi.AbstractElement;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.framework.startlevel.BundleStartLevel;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleRevisions;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.resource.Capability;

/**
 * An abstract implementation that adapts a {@link Module} to a {@link Bundle}
 *
 * @author <EMAIL>
 * @since 30-May-2012
 */
public class AbstractBundleAdaptor extends AbstractElement implements XBundle, LockManager.LockableItem {

    private final AtomicInteger bundleState = new AtomicInteger(Bundle.RESOLVED);
    private final LockSupport bundleLock = LockManager.Factory.addLockSupport(this);
    private final BundleManager bundleManager;
    private final BundleContext context;
    private final XBundleRevision brev;
    private final Module module;
    private BundleActivator bundleActivator;
    private long lastModified;

    public AbstractBundleAdaptor(BundleContext context, Module module, XBundleRevision brev) {
        if (context == null)
            throw MESSAGES.illegalArgumentNull("context");
        if (module == null)
            throw MESSAGES.illegalArgumentNull("module");
        if (brev == null)
            throw MESSAGES.illegalArgumentNull("brev");
        XBundle sysbundle = (XBundle) context.getBundle();
        this.bundleManager = sysbundle.adapt(BundleManager.class);
        this.context = context;
        this.module = module;
        this.brev = brev;

        this.lastModified = System.currentTimeMillis();
    }

    @Override
    public long getBundleId() {
        Long bundleId = brev.getAttachment(Long.class);
        return bundleId != null ? bundleId.longValue() : -1;
    }

    @Override
    public String getLocation() {
        String location = module.getIdentifier().getName();
        String slot = module.getIdentifier().getSlot();
        if (!slot.equals("main")) {
            location += ":" + slot;
        }
        return location;
    }

    @Override
    public String getSymbolicName() {
        String symbolicName = null;
        List<Capability> icaps = brev.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
        if (icaps.size() > 0) {
            XIdentityCapability icap = (XIdentityCapability) icaps.get(0);
            symbolicName = icap.getSymbolicName();
        } else {
            symbolicName = module.getIdentifier().getName();
        }
        return symbolicName;
    }

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

    @Override
    public int getState() {
        return bundleState.get();
    }

    @Override
    public Version getVersion() {
        Version version = Version.emptyVersion;
        List<Capability> icaps = brev.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
        if (icaps.size() > 0) {
            XIdentityCapability icap = (XIdentityCapability) icaps.get(0);
            version = icap.getVersion();
        } else {
            String slot = module.getIdentifier().getSlot();
            try {
                version = Version.parseVersion(slot);
            } catch (IllegalArgumentException ex) {
                // ignore
            }
        }
        return version;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return module.getClassLoader().loadClass(name);
    }

    /**
     * Required by spec:
     *
{'fraction_non_alphanumeric': 0.09869593770522563, 'fraction_numerical': 0.0053475935828877, 'mean_word_length': 3.415907207953604, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5453754', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2988, 'n_words': 768}