Search in sources :

Example 1 with ConfigurationException

use of org.osgl.exception.ConfigurationException in project actframework by actframework.

the class ConfigKeyHelper method getImpl.

<T> T getImpl(Map<String, ?> configuration, String key, String suffix, $.F0<?> defVal) {
    Object v = getValFromAliases(configuration, key, "impl", defVal);
    if (null == v)
        return null;
    if (v instanceof Class) {
        try {
            return $.newInstance((Class<T>) v, myClassLoader());
        } catch (Exception e) {
            throw new ConfigurationException(e, "Error getting implementation configuration: %s", key);
        }
    }
    if (!(v instanceof String))
        return (T) v;
    String clsName = (String) v;
    try {
        return $.newInstance(clsName, myClassLoader());
    } catch (Exception e) {
        throw new ConfigurationException(e, "Error getting implementation configuration: %s", key);
    }
}
Also used : ConfigurationException(org.osgl.exception.ConfigurationException) NotAppliedException(org.osgl.exception.NotAppliedException) ConfigurationException(org.osgl.exception.ConfigurationException)

Example 2 with ConfigurationException

use of org.osgl.exception.ConfigurationException in project actframework by actframework.

the class ConfigKeyHelper method asUri.

private static URI asUri(String s, String key) {
    boolean isAbsolute = false;
    if (s.startsWith("/") || s.startsWith(File.separator)) {
        isAbsolute = true;
    } else if (s.matches("^[a-zA-Z]:.*")) {
        isAbsolute = true;
    }
    if (isAbsolute) {
        File f = new File(s);
        if (f.exists() && f.isDirectory() && f.canRead()) {
            return f.toURI();
        }
        return null;
    }
    try {
        if (s.startsWith("..")) {
            URL url = Thread.currentThread().getContextClassLoader().getResource(".");
            if (null == url) {
                // template root starts with ".."
                return null;
            }
            String path = url.getPath();
            if (path.endsWith("/"))
                path = path + s;
            else
                path = path + "/" + s;
            return new URI(path);
        } else {
            URL url = Thread.currentThread().getContextClassLoader().getResource(s);
            return null == url ? null : url.toURI();
        }
    } catch (Exception e) {
        throw new ConfigurationException(e, "Error reading file configuration %s", key);
    }
}
Also used : ConfigurationException(org.osgl.exception.ConfigurationException) File(java.io.File) URI(java.net.URI) URL(java.net.URL) NotAppliedException(org.osgl.exception.NotAppliedException) ConfigurationException(org.osgl.exception.ConfigurationException)

Example 3 with ConfigurationException

use of org.osgl.exception.ConfigurationException in project actframework by actframework.

the class GenieInjector method foundAutoBinding.

@AnnotatedClassFinder(value = AutoBind.class, callOn = SysEventId.DEPENDENCY_INJECTOR_PROVISIONED, noAbstract = false)
public static void foundAutoBinding(final Class<?> autoBinding) {
    // check if there are manual binding (via modules) for the class already
    if (hasBinding(autoBinding)) {
        return;
    }
    final AppClassLoader cl = Act.app().classLoader();
    ClassInfoRepository repo = cl.classInfoRepository();
    ClassNode root = repo.node(autoBinding.getName());
    E.invalidConfigurationIf(null == root, "Cannot find AutoBind root: %s", autoBinding.getName());
    final Set<Class<?>> candidates = new LinkedHashSet<>();
    root.visitPublicNotAbstractSubTreeNodes(new $.Visitor<ClassNode>() {

        @Override
        public void visit(ClassNode classNode) throws Osgl.Break {
            try {
                Class<?> clazz = $.classForName(classNode.name(), cl);
                if (Env.matches(clazz)) {
                    candidates.add(clazz);
                }
            } catch (ConfigurationException e) {
                throw e;
            } catch (RuntimeException e) {
                throw new ConfigurationException(e, "Unable to auto bind on %s", autoBinding.getName());
            }
        }
    });
    if (!candidates.isEmpty()) {
        GenieInjector injector = Act.app().injector();
        // key is: set of annotations plus name
        Map<$.T2<Set<Annotation>, String>, Class<?>> multiCandidatesMap = new HashMap<>();
        for (Class<?> c : candidates) {
            BeanSpec spec = BeanSpec.of(c, injector);
            Set<Annotation> qualifiers = spec.qualifiers();
            String name = spec.name();
            $.T2<Set<Annotation>, String> key = $.T2(qualifiers, name);
            if (multiCandidatesMap.containsKey(key)) {
                throw new ConfigurationException("Unable to auto bind on %s: multiple same qualified candidates found", autoBinding);
            } else {
                multiCandidatesMap.put(key, c);
            }
        }
        for (Map.Entry<$.T2<Set<Annotation>, String>, Class<?>> entry : multiCandidatesMap.entrySet()) {
            Genie.Binder binder = new Genie.Binder(autoBinding).to(entry.getValue());
            $.T2<Set<Annotation>, String> key = entry.getKey();
            Set<Annotation> qualifiers = key._1;
            String name = key._2;
            if (!qualifiers.isEmpty()) {
                binder = binder.withAnnotation(qualifiers.toArray(new Annotation[qualifiers.size()]));
            }
            if (null != name) {
                binder.named(name);
            }
            binder.register(injector.genie());
        }
    } else {
        Act.LOGGER.warn("Unable to auto bind on %s: implementation not found", autoBinding);
    }
}
Also used : AppClassLoader(act.app.AppClassLoader) ConfigurationException(org.osgl.exception.ConfigurationException) org.osgl.$(org.osgl.$) Annotation(java.lang.annotation.Annotation)

Example 4 with ConfigurationException

use of org.osgl.exception.ConfigurationException in project actframework by actframework.

the class CliServer method start.

void start() {
    if (running()) {
        return;
    }
    try {
        serverSocket = new ServerSocket(port);
        running.set(true);
        // start server thread
        executor.submit(this);
        // start expiration monitor thread
        executor.submit(new Runnable() {

            @Override
            public void run() {
                monitorThread = Thread.currentThread();
                int expiration = app().config().cliSessionExpiration();
                while (running()) {
                    List<CliSession> toBeRemoved = new ArrayList<>();
                    for (CliSession session : sessions.values()) {
                        if (session.expired(expiration)) {
                            toBeRemoved.add(session);
                        }
                    }
                    for (CliSession session : toBeRemoved) {
                        session.stop("Your session has expired");
                        sessions.remove(session.id());
                    }
                    try {
                        Thread.sleep(60 * 1000);
                    } catch (InterruptedException e) {
                        return;
                    }
                    app().checkUpdates(false);
                }
            }
        });
        app().jobManager().on(SysEventId.ACT_START, new Runnable() {

            @Override
            public void run() {
                Act.LOGGER.info("CLI server started on port: %s", port);
            }
        });
    } catch (IOException e) {
        Throwable t = e.getCause();
        if (null != t && t.getMessage().contains("Address already in use")) {
            throw new ConfigurationException("Cannot start app, port[%s] already occupied. Possible cause: another app instance is running", port);
        }
        throw new ConfigurationException(e, "Cannot start CLI server on port: %s", port);
    }
}
Also used : ConfigurationException(org.osgl.exception.ConfigurationException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) CliSession(act.cli.CliSession)

Aggregations

ConfigurationException (org.osgl.exception.ConfigurationException)4 NotAppliedException (org.osgl.exception.NotAppliedException)2 AppClassLoader (act.app.AppClassLoader)1 CliSession (act.cli.CliSession)1 File (java.io.File)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 ServerSocket (java.net.ServerSocket)1 URI (java.net.URI)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 org.osgl.$ (org.osgl.$)1