001package org.intellimate.izou.sdk.addon;
002
003import org.intellimate.izou.activator.ActivatorModel;
004import org.intellimate.izou.addon.AddOnModel;
005import org.intellimate.izou.events.EventsControllerModel;
006import org.intellimate.izou.identification.IllegalIDException;
007import org.intellimate.izou.output.OutputExtensionModel;
008import org.intellimate.izou.output.OutputPluginModel;
009import org.intellimate.izou.sdk.Context;
010import org.intellimate.izou.sdk.contentgenerator.ContentGenerator;
011import org.intellimate.izou.sdk.util.ContextProvider;
012import org.intellimate.izou.sdk.util.Loggable;
013import org.intellimate.izou.sdk.util.LoggedExceptionCallback;
014import ro.fortsoft.pf4j.PluginWrapper;
015
016/**
017 * All AddOns must extend this Class.
018 *
019 * It will be instantiated and its registering-methods will be called by the PluginManager.
020 * This class has method for a properties-file named addOnID.properties (AddOnsID in the form: package.class)
021 */
022public abstract class AddOn implements AddOnModel, ContextProvider, Loggable, LoggedExceptionCallback {
023    private final String addOnID;
024    private Context context;
025    private PluginWrapper plugin;
026
027    /**
028     * The default constructor for AddOns
029     *
030     * @param addOnID the ID of the Plugin in the form: package.class
031     */
032    public AddOn(String addOnID) {
033        this.addOnID = addOnID;
034    }
035
036    /**
037     * This method is used to register the modules
038     */
039    @Override
040    public void register() {
041        prepare();
042        ContentGenerator[] contentGenerators = registerContentGenerator();
043        if (contentGenerators != null) {
044            for (ContentGenerator contentGenerator : contentGenerators) {
045                try {
046                    getContext().getContentGenerators().registerContentGenerator(contentGenerator);
047                } catch (IllegalIDException e) {
048                    context.getLogger().fatal("Illegal Id for Module: " + contentGenerator.getID(), e);
049                }
050            }
051        }
052
053        EventsControllerModel[] eventsControllerModels = registerEventController();
054        if (eventsControllerModels != null) {
055            for (EventsControllerModel eventsController : eventsControllerModels) {
056                try {
057                    getContext().getEvents().distributor().registerEventsController(eventsController);
058                } catch (IllegalIDException e) {
059                    context.getLogger().fatal("Illegal Id for Module: " + eventsController.getID(), e);
060                }
061            }
062        }
063
064        OutputPluginModel[] outputPluginModels = registerOutputPlugin();
065        if (outputPluginModels != null) {
066            for (OutputPluginModel outputPlugin : outputPluginModels) {
067                try {
068                    getContext().getOutput().addOutputPlugin(outputPlugin);
069                } catch (IllegalIDException e) {
070                    context.getLogger().fatal("Illegal Id for Module: " + outputPlugin.getID(), e);
071                }
072            }
073        }
074
075        OutputExtensionModel[] outputExtensionModels = registerOutputExtension();
076        if (outputExtensionModels != null) {
077            for (OutputExtensionModel outputExtension : outputExtensionModels) {
078                try {
079                    getContext().getOutput().addOutputExtension(outputExtension);
080                } catch (IllegalIDException e) {
081                    context.getLogger().fatal("Illegal Id for Module: " + outputExtension.getID(), e);
082                }
083            }
084        }
085
086        ActivatorModel[] activatorModels = registerActivator();
087        getContext().getSystem().registerInitializedListener(() -> {
088        if (activatorModels != null) {
089            for (ActivatorModel activator : activatorModels) {
090                try {
091                    getContext().getActivators().addActivator(activator);
092                } catch (IllegalIDException e) {
093                    context.getLogger().fatal("Illegal Id for Module: " + activator.getID(), e);
094                }
095            }
096        }
097        });
098    }
099
100    /**
101     * This method gets called before registering
102     */
103    public abstract void prepare();
104
105    /**
106     * Use this method to register (if needed) your Activators.
107     *
108     * @return Array containing Instances of Activators
109     */
110    public abstract ActivatorModel[] registerActivator();
111
112    /**
113     * Use this method to register (if needed) your ContentGenerators.
114     *
115     * @return Array containing Instances of ContentGenerators
116     */
117    public abstract ContentGenerator[] registerContentGenerator();
118
119    /**
120     * Use this method to register (if needed) your EventControllers.
121     *
122     * @return Array containing Instances of EventControllers
123     */
124    public abstract EventsControllerModel[] registerEventController();
125
126    /**
127     * Use this method to register (if needed) your OutputPlugins.
128     *
129     * @return Array containing Instances of OutputPlugins
130     */
131    public abstract OutputPluginModel[] registerOutputPlugin();
132
133    /**
134     * Use this method to register (if needed) your Output.
135     *
136     * @return Array containing Instances of OutputExtensions
137     */
138    public abstract OutputExtensionModel[] registerOutputExtension();
139
140    /**
141     * Internal initiation of addOn - fake constructor, comes before prepare
142     *
143     * @param context the context to initialize with
144     */
145    @Override
146    public void initAddOn(org.intellimate.izou.system.Context context) {
147        this.context = new Context(context);
148    }
149
150    /**
151     * Returns the Context of the AddOn.
152     *
153     * Context provides some general Communications.
154     *
155     * @return an instance of Context.
156     */
157    @Override
158    public Context getContext() {
159        return context;
160    }
161
162    /**
163     * Gets the associated Plugin.
164     *
165     * @return the Plugin.
166     */
167    @Override
168    public PluginWrapper getPlugin() {
169        return plugin;
170    }
171
172    /**
173     * Sets the Plugin IF it is not already set.
174     *
175     * @param plugin the plugin
176     */
177    @Override
178    public void setPlugin(PluginWrapper plugin) {
179        this.plugin = plugin;
180    }
181
182    /**
183     * An ID must always be unique.
184     * A Class like Activator or OutputPlugin can just provide their .class.getCanonicalName()
185     * If you have to implement this interface multiple times, just concatenate unique Strings to
186     * .class.getCanonicalName()
187     *
188     * @return A String containing an ID
189     */
190    @Override
191    public String getID() {
192        return addOnID;
193    }
194}