Search in sources :

Example 1 with IServiceContext

use of org.mapleir.serviceframework.api.IServiceContext in project maple-ir by LLVM-but-worse.

the class STest method main.

public static void main(String[] args) {
    IServiceRegistry reg = ServiceHelper.getGlobalServiceRegistry();
    IServiceContext cxt = ServiceHelper.getGlobalServiceContext();
    IServiceFactory<Person> personFactory = new IServiceFactory<Person>() {

        @Override
        public Person create(IPropertyDictionary dict) {
            IProperty<String> nameProp = dict.find(String.class, "name");
            IProperty<Boolean> genderProp = dict.find(boolean.class, "ismale");
            String name = nameProp.getValue();
            if (genderProp.getValue()) {
                return new Male(name);
            } else {
                return new Female(name);
            }
        }
    };
    reg.registerServiceFactory(cxt, Person.class, personFactory);
    // some other module that doesnt have impl (male/female/base) classes, only Person
    IServiceReference<Person> ref = reg.getServiceReference(cxt, Person.class);
    IPropertyDictionary settings = PropertyHelper.createDictionary();
    settings.put(new BooleanProperty(PropertyHelper.BASIC_SYNCHRONISED_DICTIONARY_OPT, true));
    {
        IPropertyDictionary dict = PropertyHelper.createDictionary(settings);
        System.out.println(dict);
        dict.put(new StringProperty("name", "", "bilb"));
        dict.put(new BooleanProperty("ismale", true));
        Person p = reg.getService(ref, dict);
        System.out.println(p);
    }
    {
        IPropertyDictionary dict = PropertyHelper.createDictionary();
        System.out.println(dict);
        dict.put(new StringProperty("name", "", "theresa NAY"));
        dict.put(new BooleanProperty("ismale", false));
        Person p = reg.getService(ref, dict);
        System.out.println(p);
    }
    reg.ungetService(ref);
}
Also used : IPropertyDictionary(org.mapleir.propertyframework.api.IPropertyDictionary) BooleanProperty(org.mapleir.propertyframework.impl.BooleanProperty) IServiceRegistry(org.mapleir.serviceframework.api.IServiceRegistry) StringProperty(org.mapleir.propertyframework.impl.StringProperty) IServiceContext(org.mapleir.serviceframework.api.IServiceContext) IServiceFactory(org.mapleir.serviceframework.api.IServiceFactory)

Aggregations

IPropertyDictionary (org.mapleir.propertyframework.api.IPropertyDictionary)1 BooleanProperty (org.mapleir.propertyframework.impl.BooleanProperty)1 StringProperty (org.mapleir.propertyframework.impl.StringProperty)1 IServiceContext (org.mapleir.serviceframework.api.IServiceContext)1 IServiceFactory (org.mapleir.serviceframework.api.IServiceFactory)1 IServiceRegistry (org.mapleir.serviceframework.api.IServiceRegistry)1