<commit_before>package org.osgi.impl.bundle.midletcontainer;

import java.io.*;
import java.util.*;

import javax.microedition.midlet.MIDlet;

import org.osgi.framework.*;
import org.osgi.service.application.ApplicationDescriptor;
import org.osgi.service.application.ApplicationHandle;
import org.osgi.service.log.LogService;



public class MidletContainer implements BundleListener, ServiceListener {
	private BundleContext	        bc;
	private Vector			          installedMidletBundles;
	private Hashtable	           	bundleDescriptorHash;
	private OATContainerInterface oat;
	ServiceReference              oatRef = null;

	public MidletContainer(BundleContext bc) throws Exception {
		this.bc = bc;
		bundleDescriptorHash = new Hashtable();
		installedMidletBundles = new Vector();
		
		oatRef = bc .getServiceReference(OATContainerInterface.class.getName());
    if (oatRef == null)
	    throw new Exception("Cannot start the MidletContainer as OAT is not running!");
    oat = (OATContainerInterface) bc.getService(oatRef);
    if (oat == null)
	    throw new Exception("Cannot start the MidletContainer as OAT is not running!");
		
		bc.addBundleListener( this );
		
		if( !oatRef.getBundle().equals( bc.getBundle() ) ) /* OAT is provided by me? */
			bc.addServiceListener( this );
		
		Bundle bundles[] = bc.getBundles();
		for (int i = 0; i < bundles.length; i++) {
			if (!installedMidletBundles.contains( bundles[ i ] ) && isMidletBundle( bundles[ i ] ) )
				installMidletBundle(bundles[i]);
		}
	}

	public void installMidletBundle(Bundle bundle) throws IOException, Exception {
		if (installedMidletBundles.contains( bundle ))
			return;
		

    ApplicationDescriptor appDescs[] = parseMidletHeaders(bundle);
		if (appDescs == null)
			throw new Exception("Not a valid MIDlet bundle!");

		String startClasses[] = new String [ appDescs.length ];
		for( int i=0; i != appDescs.length; i++ )
			startClasses[ i ] = ((MidletDescriptor)appDescs[ i ]).getStartClass();
		
		oat.registerOATBundle( bundle, startClasses );

		registerApplicationDescriptors( bundle );		
		installedMidletBundles.add( bundle );
	}

	public void stop() throws Exception {
		
		for (int i = 0; i != installedMidletBundles.size(); i++)
			unregisterApplicationDescriptors((Bundle)installedMidletBundles.get(i));

		bc.ungetService( oatRef );
		bc.removeBundleListener( this );
	}

	private void terminateApplications( MidletDescriptor descs[] ) {<commit_msg>FIXED: not allowing to start resolved applications<commit_after>package org.osgi.impl.bundle.midletcontainer;

import java.io.*;
import java.util.*;

import javax.microedition.midlet.MIDlet;

import org.osgi.framework.*;
import org.osgi.service.application.ApplicationDescriptor;
import org.osgi.service.application.ApplicationHandle;
import org.osgi.service.log.LogService;



public class MidletContainer implements BundleListener, ServiceListener {
	private BundleContext	        bc;
	private Vector			          installedMidletBundles;
	private Hashtable	           	bundleDescriptorHash;
	private OATContainerInterface oat;
	ServiceReference              oatRef = null;

	public MidletContainer(BundleContext bc) throws Exception {
		this.bc = bc;
		bundleDescriptorHash = new Hashtable();
		installedMidletBundles = new Vector();
		
		oatRef = bc .getServiceReference(OATContainerInterface.class.getName());
    if (oatRef == null)
	    throw new Exception("Cannot start the MidletContainer as OAT is not running!");
    oat = (OATContainerInterface) bc.getService(oatRef);
    if (oat == null)
	    throw new Exception("Cannot start the MidletContainer as OAT is not running!");
		
		bc.addBundleListener( this );
		
		if( !oatRef.getBundle().equals( bc.getBundle() ) ) /* OAT is provided by me? */
			bc.addServiceListener( this );
		
		Bundle bundles[] = bc.getBundles();
		for (int i = 0; i < bundles.length; i++) {
			if (!installedMidletBundles.contains( bundles[ i ] ) && isMidletBundle( bundles[ i ] ) )
				installMidletBundle(bundles[i]);
		}
	}

	public void installMidletBundle(Bundle bundle) throws IOException, Exception {
		if (installedMidletBundles.contains( bundle ))
			return;
		

    ApplicationDescriptor appDescs[] = parseMidletHeaders(bundle);
		if (appDescs == null)
			throw new Exception("Not a valid MIDlet bundle!");

		String startClasses[] = new String [ appDescs.length ];
		for( int i=0; i != appDescs.length; i++ )
			startClasses[ i ] = ((MidletDescriptor)appDescs[ i ]).getStartClass();
		
		oat.registerOATBundle( bundle, startClasses );

		if( bundle.getState() == Bundle.ACTIVE )
			registerApplicationDescriptors( bundle );
		
		installedMidletBundles.add( bundle );
	}

	public void stop() throws Exception {
		
		for (int i = 0; i != installedMidletBundles.size(); i++)
			unregisterApplicationDescriptors((Bundle)installedMidletBundles.get(i));

		bc.ungetService( oatRef );
		bc.removeBundleListener( this );
	}

	private void terminateApplications( MidletDescriptor descs[] ) {
{'fraction_non_alphanumeric': 0.10921158487530169, 'fraction_numerical': 0.0012067578439259854, 'mean_word_length': 4.2568710359408035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2684143', 'n_tokens_mistral': 1649, 'n_tokens_neox': 1438, 'n_words': 375}