Package net.xeoh.plugins.diagnosis.local.options.status

Examples of net.xeoh.plugins.diagnosis.local.options.status.OptionInfo


       
       
        DiagnosisChannelUtil<String> c = new DiagnosisChannelUtil<String>(null);
        c.status("null");
        c.status("null", "a", "b");
        c.status("null", new OptionInfo("a", "b"));

    }
View Full Code Here


     *
     * @see net.xeoh.plugins.base.PluginManager#addPluginsFrom(java.net.URI,
     * net.xeoh.plugins.base.options.AddPluginsFromOption[])
     */
    public PluginManager addPluginsFrom(final URI url, final AddPluginsFromOption... options) {
        this.diagnosis.channel(PluginManagerTracer.class).status("add/start", new OptionInfo("url", url));
        if(url == null) return this;
       
        // Add from the given location
        if (!this.classPathManager.addFromLocation(url, options)) {
            this.diagnosis.channel(PluginManagerTracer.class).status("add/nohandler", new OptionInfo("url", url));
        }

        // Check if we should print a report?
        if ($(options).get(OptionReportAfter.class, null) != null)
            this.pluginRegistry.report();

        this.diagnosis.channel(PluginManagerTracer.class).status("add/end", new OptionInfo("url", url));
        return this;
    }
View Full Code Here

    public <P extends Plugin> P getPlugin(final Class<P> requestedPlugin,
                                          GetPluginOption... options) {
        // Report our request.
        if (this.diagnosis != null) {
            String name = requestedPlugin == null ? "null" : requestedPlugin.getCanonicalName();
            this.diagnosis.channel(PluginManagerTracer.class).status("get/start", new OptionInfo("plugin", name));
        }

        // We don't handle null values.
        if (requestedPlugin == null) {
            this.diagnosis.channel(PluginManagerTracer.class).status("get/end", new OptionInfo("return", null));
            return null;
        }

        // Sanity check.
        if (!requestedPlugin.isInterface()) {
            this.diagnosis.channel(PluginManagerTracer.class).status("get/onlyinterface", new OptionInfo("plugin", requestedPlugin.getCanonicalName()));
            this.diagnosis.channel(PluginManagerTracer.class).status("get/end", new OptionInfo("return", null));

            System.err.println("YOU MUST NOT call getPlugin() with a concrete class; only interfaces are");
            System.err.println("supported for lookup. This means do not call getPlugin(MyPluginImpl.class),");
            System.err.println("but rather getPlugin(MyPlugin.class)!");
            return null;
        }

        // Used to process the options
        final OptionUtils<GetPluginOption> ou = new OptionUtils<GetPluginOption>(options);

        // We use this one to select the plugin
        PluginSelector<P> pluginSelector = null;

        // Check our options. In case we have a plugin selector, only use the selector
        if (ou.contains(OptionPluginSelector.class)) {
            pluginSelector = ou.get(OptionPluginSelector.class).getSelector();
        } else {
            // Capabilites we require
            final String capabilites[] = ou.get(OptionCapabilities.class, new OptionCapabilities()).getCapabilities();

            // Get caps as list
            final Collection<String> caps = Arrays.asList(capabilites);

            // Create our own selector
            pluginSelector = new PluginSelector<P>() {
                public boolean selectPlugin(final Plugin plugin) {

                    // In case we have caps do special handling and don't return the next
                    // best plugin
                    if (caps.size() > 0) {
                        Collection<String> pcaps = PluginManagerImpl.this.information.getInformation(Information.CAPABILITIES, plugin);

                        // Check the plugin has them all
                        if (pcaps.containsAll(caps)) return true;
                        return false;
                    }

                    return true;
                }
            };
        }

        // Check for each plugin if it matches
        for (final Plugin plugin : this.pluginRegistry.getAllPlugins()) {
            if (this.diagnosis != null)
                this.diagnosis.channel(PluginManagerTracer.class).status("get/considering", new OptionInfo("plugin", plugin.toString()));

            // Check the meta information for this plugin. We only want active classes
            final PluginMetaInformation metaInformation = this.pluginRegistry.getMetaInformationFor(plugin);

            // Plugins not active are not considered
            if (metaInformation.pluginStatus != PluginStatus.ACTIVE) continue;

            // Check if the plugin can be assigned to the requested class
            if (requestedPlugin.isAssignableFrom(plugin.getClass())) {
                if (pluginSelector.selectPlugin((P) plugin)) {
                    if (this.diagnosis != null)
                        this.diagnosis.channel(PluginManagerTracer.class).status("get/end", new OptionInfo("return", plugin.toString()));
                    return (P) plugin;
                }
            }
        }

        if (this.diagnosis != null)
            this.diagnosis.channel(PluginManagerTracer.class).status("get/end", new OptionInfo("return", null));
        return null;
    }
View Full Code Here

            return;
        }

        // Destroy plugins in a random order
        for (final Plugin p : this.pluginRegistry.getAllPlugins()) {
            this.diagnosis.channel(PluginManagerTracer.class).status("shutdown/destroy", new OptionInfo("plugin", p.getClass().getCanonicalName()));
            this.spawner.destroyPlugin(p, this.pluginRegistry.getMetaInformationFor(p));
        }

        // Curtains down, lights out.
        this.pluginRegistry.clear();
View Full Code Here

        // Convert the options we have
        final OptionInfo[] options = $(infos).forEach(new Fn<Serializable, OptionInfo>() {
            @Override
            public OptionInfo f(Serializable... arg0) {
                return new OptionInfo(arg0[0].toString(), arg0[1]);
            }
        }, 2).array(OptionInfo.class);
       
        this.object.status(value, options);
    }
View Full Code Here

            @Override
            public void nextEntry(Entry entry) {
                // Convert the entry to a status events
                final List<OptionInfo> infos = new ArrayList<OptionInfo>();
                for (String value : entry.additionalInfo.keySet()) {
                    infos.add(new OptionInfo(value, (Serializable) entry.additionalInfo.get(value)));
                }

                final DiagnosisStatusImpl<?> event = new DiagnosisStatusImpl(entry.channel, (Serializable) entry.value, entry.date, $(infos).array(OptionInfo.class));
                listener.onStatusChange((DiagnosisStatus) event);
            }
View Full Code Here

     * @param metaInformation
     */
    public void destroyPlugin(final Plugin plugin,
                                 final PluginMetaInformation metaInformation) {

        log("destroy/start", new OptionInfo("plugin", plugin.getClass().getCanonicalName()));
       
        // Halt all timer tasks
        for (final TimerTask timerTask : metaInformation.timerTasks) {
            timerTask.cancel();
        }

        // Halt all timers
        for (final java.util.Timer timer : metaInformation.timers) {
            timer.cancel();
        }

        // Halt all threads
        for (final java.lang.Thread thread : metaInformation.threads) {
            // TODO: Maybe not the best way to terminate.
            try {
                thread.interrupt();
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }

        // Call shutdown hooks
        callShutdownMethods(plugin);
        log("destroy/end", new OptionInfo("plugin", plugin.getClass().getCanonicalName()));       
    }
View Full Code Here

     * @param c Class to spawn from.
     * @return .
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public SpawnResult spawnPlugin(final Class c) {
        log("spawn/start", new OptionInfo("plugin", c.getCanonicalName()));

        // Used for time measurements.
        final long startTime = System.nanoTime();
        final java.util.Timer timer = new java.util.Timer();
        final TimerTask lateMessage = new TimerTask() {
            @Override
            public void run() {
                log("spawn/timeout/toolong", new OptionInfo("plugin", c.getCanonicalName()));
            }
        };

        // Finally load and register plugin
        try {
            // Schedule late message. (TODO: Make this configurable)
            timer.schedule(lateMessage, 250);

            // Instanciate the plugin
            final Constructor constructor = c.getDeclaredConstructor();
            constructor.setAccessible(true);
            final Plugin spawnedPlugin = (Plugin) constructor.newInstance();
           
            // In here spawning of the plugin worked
            final SpawnResult spawnResult = new SpawnResult(spawnedPlugin);
            spawnResult.metaInformation.pluginStatus = PluginStatus.SPAWNED;
            spawnResult.metaInformation.spawnTime = System.currentTimeMillis();

            // Finally load and register plugin
            try {

                new InjectHandler(this.pluginManager).init(spawnedPlugin);

                // Obtain all methods
                final Method[] methods = getMethods(c);

                // 2. Call all init methods
                final boolean initStatus = callInitMethods(spawnedPlugin, methods);
                if (initStatus == false) {
                    spawnResult.metaInformation.pluginStatus = PluginStatus.FAILED;
                    return spawnResult;
                }

                // Initialization complete
                spawnResult.metaInformation.pluginStatus = PluginStatus.INITIALIZED;

                // 3. Spawn all threads
                spawnThreads(spawnResult, methods);

                // 4. Spawn timer
                spawnTimer(spawnResult, methods);

                // 5. Obtain PluginLoaded methods
                obtainPluginLoadedMethods(spawnResult, methods);

                // Currently running
                spawnResult.metaInformation.pluginStatus = PluginStatus.ACTIVE;

                log("spawn/end", new OptionInfo("plugin", c.getCanonicalName()));       
                return spawnResult;
            } catch (final Throwable e) {
                log("spawn/exception/init", new OptionInfo("plugin", c.getCanonicalName()));
                e.printStackTrace();
                Throwable cause = e.getCause();
                while (cause != null) {
                    cause.printStackTrace();
                    cause = cause.getCause();
                }
            }
            return null;

        } catch (final Throwable e) {
            log("spawn/exception/construct", new OptionInfo("plugin", c.getCanonicalName()));
            e.printStackTrace();
            Throwable cause = e.getCause();
            while (cause != null) {
                cause.printStackTrace();
                cause = cause.getCause();
            }
        } finally {
            // Halt the late message
            timer.cancel();

            final long stopTime = System.nanoTime();
            final long delta = (stopTime - startTime) / 1000;
            log("spawn/duration", new OptionInfo("plugin", c.getCanonicalName()), new OptionInfo("time", ""+ delta));
        }
       
        log("spawn/end/abnormal", new OptionInfo("plugin", c.getCanonicalName()));       
        return null;
    }
View Full Code Here

     *
     *
     */
    private boolean callInitMethods(final Plugin spawnedPlugin, final Method[] methods)
                                                                                          throws IllegalAccessException {
        log("callinit/start", new OptionInfo("plugin", spawnedPlugin.getClass().getCanonicalName()));       
        // final Class<? extends Plugin> spawnClass = spawnedPlugin.getClass();


        for (final Method method : methods) {
            log("callinit/method", new OptionInfo("method", method.getName()));       

            // Init methods will be marked by the corresponding annotation.
            final Init annotation = method.getAnnotation(Init.class);
            if (annotation != null) {
                log("callinit/method/initannotation", new OptionInfo("method", method.getName()));       

                try {
                    final Object invoke = method.invoke(spawnedPlugin, new Object[0]);
                    if (invoke != null && invoke instanceof Boolean) {
                        // Check if any init method returns false.
                        if (((Boolean) invoke).booleanValue() == false) return false;
                    }
                } catch (final IllegalArgumentException e) {
                    log("callinit/exception/illegalargument", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    log("callinit/end/abnormal", new OptionInfo("plugin", spawnedPlugin.getClass().getCanonicalName()));                           
                    e.printStackTrace();
                    return false;
                } catch (final InvocationTargetException e) {
                    log("callinit/exception/invocationtargetexception", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    log("callinit/end/abnormal", new OptionInfo("plugin", spawnedPlugin.getClass().getCanonicalName()));                           
                    e.printStackTrace();
                    return false;
                } catch (final Exception e) {
                    log("callinit/exception/exception", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    log("callinit/end/abnormal", new OptionInfo("plugin", spawnedPlugin.getClass().getCanonicalName()));                                               
                    e.printStackTrace();
                    return false;
                }
            }
        }
       
        log("callinit/end", new OptionInfo("plugin", spawnedPlugin.getClass().getCanonicalName()));                                   
        return true;
    }
View Full Code Here

    /**
     * @param plugin
     */
    private void callShutdownMethods(final Plugin plugin) {
        log("callshutdown/start", new OptionInfo("plugin", plugin.getClass().getCanonicalName()));               
        final Class<? extends Plugin> spawnClass = plugin.getClass();
        final Method[] methods = spawnClass.getMethods();


        for (final Method method : methods) {
            log("callshutdown/method", new OptionInfo("method", method.getName()));       

            // Init methods will be marked by the corresponding annotation.
            final Shutdown annotation = method.getAnnotation(Shutdown.class);
            if (annotation != null) {
                log("callshutdown/method/shutdownannotation", new OptionInfo("method", method.getName()));       

                try {
                    method.invoke(plugin, new Object[0]);
                } catch (final IllegalArgumentException e) {
                    log("callshutdown/exception/illegalargument", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    e.printStackTrace();
                } catch (final InvocationTargetException e) {
                    log("callinit/exception/invocationtargetexception", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    e.printStackTrace();
                } catch (final Exception e) {
                    log("callshutdown/exception/exception", new OptionInfo("method", method.getName()), new OptionInfo("message", e.getMessage()));       
                    e.printStackTrace();
                }
            }
        }
       
        log("callshutdown/end", new OptionInfo("plugin", plugin.getClass().getCanonicalName()));                                   
        return;
    }
View Full Code Here

TOP

Related Classes of net.xeoh.plugins.diagnosis.local.options.status.OptionInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.