/** * Registers all annotations specifying in {@link ComponentServiceContext} * * @param serviceHandler the {@link ServiceHandler} * @param annotation the {@link ComponentServiceContext} annotation. * @param getProvider if <b>true</b>, the suitable validator will be gotten from the given {@link * Provider}, if <b>false</b>, the instance will be instantiated basing on <code> * default constructor</code> specifying in corresponding object class. * @param provider the specified {@link Provider} container, if <code>getInject argument</code> is * <b>true</b> and provider is null, one runtime-exception will be thrown. */ @SuppressWarnings("unchecked") private static void registerAnnotationInCSC( ServiceHandler serviceHandler, ComponentServiceContext annotation, boolean getProvider, Provider provider) { if (getProvider == true && provider == null) { throw new JGentleRuntimeException("Provider must not be null while getProvider is true."); } Class<Annotation>[] annoList = (Class<Annotation>[]) annotation.annotations(); Class<?>[] validators = annotation.validators(); if (annoList.length != 0) { if (annoList.length < validators.length) { throw new JGentleRuntimeException( "invalid validators configuration in annotation: " + annotation); } for (int i = 0; i < annoList.length; i++) { if (i < validators.length) { AnnotationValidator<Annotation> validator; if (getProvider == false) validator = (AnnotationValidator<Annotation>) ReflectUtils.createInstance(validators[i]); else { validator = (AnnotationValidator<Annotation>) provider.getBean(validators[i]); } serviceHandler.registerAnnotation(annoList[i], validator); } else { serviceHandler.registerAnnotation(annoList[i]); } } } }
/** * Registers all system annotations. * * @param serviceHandler the given {@link ServiceHandler} */ private static void registerAnnotations(ServiceHandler serviceHandler) { // Đăng kí thông tin các annotation của core JGentle vào danh sách // annotation registered serviceHandler.registerAnnotations(RegisterAnnotationInjecting.class); // Đăng kí thông tin tất cả các annotation của AOP System vào danh sách // annotation registered. serviceHandler.registerAnnotations(RegisterAnnotationAOP.class); // Đăng kí thông tin tất cả các annotation của JGentle Context vào danh // sách annotation registered. serviceHandler.registerAnnotations(RegisterAnnotationContext.class); }
/** * Builds the context. * * @param serviceHandler the aoh * @param serviceProvider the service provider * @param configurations the configurations * @return the context */ protected static Context buildContext( ServiceHandler serviceHandler, boolean serviceProvider, Configurable... configurations) { List<Map<String, Object>> OLArray = new ArrayList<Map<String, Object>>(); ArrayList<Configurable> cfgInstanceList = new ArrayList<Configurable>(); for (Configurable config : configurations) { try { config.setDefinitionManager(serviceHandler.getDefinitionManager()); // Thực thi các xử lý được chỉ định bởi ATC config.initAnnotationConfig(); cfgInstanceList.add(config); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } OLArray.add(config.getOptionsList()); } Context result = null; if (!serviceProvider) { result = new ProviderCoreCreator(serviceHandler, OLArray); } else { result = new ServiceProviderImpl(serviceHandler, OLArray); } ((Provider) result).setConfigInstances(cfgInstanceList); return result; }
/** * Instantiates a {@link ServiceProvider}. * * @param configClasses a specified configurable class or a set of specified configurable classes * @param argsType the argument types of specified constructor of configurable class * @param args the argument objects need to be passed to constructor of configurable class. * @return the service provider */ @SuppressWarnings("unchecked") public static ServiceProvider buildServiceProvider( Class<?>[] argsType, Object[] args, Class<? extends Configurable>... configClasses) { /* * Khởi tạo services context */ ServiceProvider result = (ServiceProvider) buildContext(true, argsType, args, configClasses); /* * Thực thi các xử lý trước khi khởi tạo CSC */ List<Configurable> configInstances = result.getConfigInstances(); for (BeforeInitContext obj : JGentle.beforeInitContextList) { obj.beforeInitContext( result, configInstances.toArray(new Configurable[configInstances.size()])); } // Thực thi init trên csc // Thực thi init method trên mỗi CSC của services context vừa khởi tạo. Collection<ComponentServiceContextType<Configurable>> csclist = result.getCSCList().values(); for (ComponentServiceContextType<Configurable> csc : csclist) { invokeCSCInit(result, csc, configInstances); } /* * Thực thi các xử lý khởi tạo CSC */ List<Class<? extends ComponentServiceContextType<?>>> cscClassList = null; cscClassList = new ArrayList<Class<? extends ComponentServiceContextType<?>>>(); for (Configurable instance : configInstances) { cscClassList.addAll(instance.getCscClassList()); } // Duyệt qua từng danh sách Object Class của danh sách các CSC class. for (Class<? extends ComponentServiceContextType<?>> clazz : cscClassList) { Definition defCSC = result.getDefinitionManager().getDefinition(clazz); // Khởi tạo CSC và thực thi init ComponentServiceContextType<Configurable> comp = null; comp = (ComponentServiceContextType<Configurable>) result.getBean(clazz); invokeCSCInit(result, comp, configInstances); /* * Xử lý thông tin chỉ định trong @ComponentServiceContext nếu có */ ComponentServiceContext anno = null; if (defCSC.isAnnotationPresent(ComponentServiceContext.class)) { anno = defCSC.getAnnotation(ComponentServiceContext.class); } if (anno != null) { // Thực thi đăng kí annotation nếu có. if (!anno.beforeConfigure()) { JGentle.registerAnnotationInCSC(result.getServiceHandler(), anno, true, result); } // Khởi tạo và đăng kí service Class Class<? extends ServiceClass> scClazz = anno.serviceClass(); if (!result.getDefinitionManager().containsDefinition(scClazz)) { result.getDefinitionManager().loadDefinition(scClazz); } if (result .getDefinitionManager() .getDefinition(scClazz) .isAnnotationPresent(BeanServices.class)) { throw new JGentleRuntimeException( "Service Class " + scClazz.getName() + " must be annotated with @BeanServices annotation !"); } String domain = result .getDefinitionManager() .getDefinition(scClazz) .getAnnotation(BeanServices.class) .domain(); ServiceHandler aoh = result.getServiceHandler(); if (!aoh.containsDomain(domain)) { try { aoh.newDomain(domain); } catch (JGentleException e) { e.printStackTrace(); } } aoh.addService(result, scClazz, domain); } // Thực thi add CSC vào ServiceProvider hiện hành. result.addCSContext( anno == null || (anno != null && anno.value().equals(NullClass.class)) ? clazz : anno.value(), comp); } /* * Xóa bỏ toàn bộ các beforeInitContext object */ beforeInitContextList.clear(); return result; }