Search in sources :

Example 1 with ApplicationConfigurationLoader

use of com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader in project baseio by generallycloud.

the class ApplicationBootstrapEngine method bootstrap.

@Override
public void bootstrap(String rootPath, boolean deployModel) throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    Properties serverProperties = FileUtil.readPropertiesByCls("server.properties");
    ServerConfiguration sc = new ServerConfiguration();
    new PropertiesSCLoader("SERVER").loadConfiguration(sc, serverProperties);
    ApplicationContext applicationContext = new ApplicationContext(rootPath);
    applicationContext.setDeployModel(deployModel);
    SocketChannelContext channelContext = new NioSocketChannelContext(sc);
    // SocketChannelContext channelContext = new AioSocketChannelContext(sc);
    SocketChannelAcceptor acceptor = new SocketChannelAcceptor(channelContext);
    try {
        Properties intfProperties = FileUtil.readPropertiesByCls("intf.properties");
        applicationContext.setBlackIPs(loadBlackIPs());
        applicationContext.setChannelContext(channelContext);
        ApplicationConfigurationLoader acLoader = loadConfigurationLoader(intfProperties.getProperty("intf.ApplicationConfigurationLoader"));
        ApplicationExtLoader applicationExtLoader = loadApplicationExtLoader(intfProperties.getProperty("intf.ApplicationExtLoader"));
        ApplicationContextEnricher enricher = loadApplicationContextEnricher(intfProperties.getProperty("intf.ApplicationContextEnricher"));
        applicationContext.setApplicationExtLoader(applicationExtLoader);
        applicationContext.setApplicationConfigurationLoader(acLoader);
        enricher.enrich(applicationContext);
        channelContext.setIoEventHandleAdaptor(new ApplicationIoEventHandle(applicationContext));
        if (sc.isSERVER_ENABLE_SSL()) {
            if (!StringUtil.isNullOrBlank(sc.getSERVER_CERT_KEY())) {
                File certificate = FileUtil.readFileByCls(sc.getSERVER_CERT_CRT(), classLoader);
                File privateKey = FileUtil.readFileByCls(sc.getSERVER_CERT_KEY(), classLoader);
                SslContext sslContext = SSLUtil.initServer(privateKey, certificate);
                channelContext.setSslContext(sslContext);
            } else {
                String keystoreInfo = sc.getSERVER_SSL_KEYSTORE();
                if (StringUtil.isNullOrBlank(keystoreInfo)) {
                    throw new IllegalArgumentException("ssl enabled,but no config for");
                }
                String[] params = keystoreInfo.split(";");
                if (params.length != 4) {
                    throw new IllegalArgumentException("SERVER_SSL_KEYSTORE config error");
                }
                File storeFile = FileUtil.readFileByCls(params[0], classLoader);
                SslContext sslContext = SSLUtil.initServer(storeFile, params[1], params[2], params[3]);
                channelContext.setSslContext(sslContext);
            }
        }
        sc.setSERVER_PORT(getServerPort(sc.getSERVER_PORT(), sc.isSERVER_ENABLE_SSL()));
        acceptor.bind();
    } catch (Throwable e) {
        Logger logger = LoggerFactory.getLogger(getClass());
        logger.error(e.getMessage(), e);
        CloseUtil.unbind(acceptor);
    }
}
Also used : PropertiesSCLoader(com.generallycloud.baseio.configuration.PropertiesSCLoader) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) ApplicationIoEventHandle(com.generallycloud.baseio.container.ApplicationIoEventHandle) Properties(com.generallycloud.baseio.common.Properties) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) Logger(com.generallycloud.baseio.log.Logger) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ApplicationContext(com.generallycloud.baseio.container.ApplicationContext) ApplicationExtLoader(com.generallycloud.baseio.container.ApplicationExtLoader) ApplicationContextEnricher(com.generallycloud.baseio.container.ApplicationContextEnricher) ApplicationConfigurationLoader(com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader) File(java.io.File) SocketChannelAcceptor(com.generallycloud.baseio.acceptor.SocketChannelAcceptor) SslContext(com.generallycloud.baseio.component.ssl.SslContext)

Example 2 with ApplicationConfigurationLoader

use of com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader in project baseio by generallycloud.

the class RoleManager method initialize.

@Override
public void initialize(ApplicationContext context, Configuration config) throws Exception {
    ApplicationConfigurationLoader acLoader = context.getAcLoader();
    PermissionConfiguration permissionConfiguration = acLoader.loadPermissionConfiguration(getClass().getClassLoader());
    List<Configuration> permissionConfigurations = permissionConfiguration.getPermissions();
    List<Configuration> roleConfigurations = permissionConfiguration.getRoles();
    if (permissionConfigurations == null || permissionConfigurations.size() == 0 || roleConfigurations == null || roleConfigurations.size() == 0) {
        throw new Error("没有加载到角色配置");
    }
    IntObjectHashMap<Permission> permissions = new IntObjectHashMap<>();
    for (Configuration c : permissionConfigurations) {
        Permission p = new Permission();
        p.setDescription(c.getParameter("description"));
        p.setFrequency(c.getIntegerParameter("frequency"));
        p.setPermissionAPI(c.getParameter("permissionAPI"));
        p.setPermissionId(c.getIntegerParameter("permissionId"));
        permissions.put(p.getPermissionId(), p);
    }
    IntObjectHashMap<Role> roles = new IntObjectHashMap<>();
    List<Role> roleList = new ArrayList<>();
    for (Configuration c : roleConfigurations) {
        Role r = new Role();
        r.setDescription(c.getParameter("description"));
        r.setRoleId(c.getIntegerParameter("roleId"));
        r.setRoleName(c.getParameter("roleName"));
        JSONArray array = c.getJSONArray("children");
        if (array != null && !array.isEmpty()) {
            List<Integer> _children = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                _children.add(array.getInteger(i));
            }
            r.setChildren(_children);
        }
        array = c.getJSONArray("permissions");
        if (array != null && !array.isEmpty()) {
            List<Integer> _permissions = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                _permissions.add(array.getInteger(i));
            }
            r.setPermissions(_permissions);
        }
        roles.put(r.getRoleId(), r);
        roleList.add(r);
    }
    reflectPermission(roleList, roles, permissions);
}
Also used : PermissionConfiguration(com.generallycloud.baseio.container.configuration.PermissionConfiguration) Configuration(com.generallycloud.baseio.container.configuration.Configuration) IntObjectHashMap(com.generallycloud.baseio.collection.IntObjectHashMap) ArrayList(java.util.ArrayList) JSONArray(com.alibaba.fastjson.JSONArray) PermissionConfiguration(com.generallycloud.baseio.container.configuration.PermissionConfiguration) ApplicationConfigurationLoader(com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader)

Aggregations

ApplicationConfigurationLoader (com.generallycloud.baseio.container.configuration.ApplicationConfigurationLoader)2 JSONArray (com.alibaba.fastjson.JSONArray)1 SocketChannelAcceptor (com.generallycloud.baseio.acceptor.SocketChannelAcceptor)1 IntObjectHashMap (com.generallycloud.baseio.collection.IntObjectHashMap)1 Properties (com.generallycloud.baseio.common.Properties)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 SslContext (com.generallycloud.baseio.component.ssl.SslContext)1 PropertiesSCLoader (com.generallycloud.baseio.configuration.PropertiesSCLoader)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 ApplicationContext (com.generallycloud.baseio.container.ApplicationContext)1 ApplicationContextEnricher (com.generallycloud.baseio.container.ApplicationContextEnricher)1 ApplicationExtLoader (com.generallycloud.baseio.container.ApplicationExtLoader)1 ApplicationIoEventHandle (com.generallycloud.baseio.container.ApplicationIoEventHandle)1 Configuration (com.generallycloud.baseio.container.configuration.Configuration)1 PermissionConfiguration (com.generallycloud.baseio.container.configuration.PermissionConfiguration)1 Logger (com.generallycloud.baseio.log.Logger)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1