mirror of https://github.com/pf4j/pf4j.git
Decebal Suiu
11 years ago
9 changed files with 278 additions and 96 deletions
@ -0,0 +1,45 @@
|
||||
/* |
||||
* Copyright 2014 Decebal Suiu |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with |
||||
* the License. You may obtain a copy of the License in the LICENSE file, or at: |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package ro.fortsoft.pf4j; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* The default implementation for ExtensionFactory. |
||||
* It uses Class.newInstance() method. |
||||
* |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class DefaultExtensionFactory implements ExtensionFactory { |
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFactory.class); |
||||
|
||||
/** |
||||
* Creates an extension instance. If an error occurs than that error is logged and the method returns null. |
||||
* @param extensionClass |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public Object create(Class<?> extensionClass) { |
||||
log.debug("Create instance for extension '{}'", extensionClass.getName()); |
||||
try { |
||||
return extensionClass.newInstance(); |
||||
} catch (Exception e) { |
||||
log.error(e.getMessage(), e); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,69 @@
|
||||
/* |
||||
* Copyright 2014 Decebal Suiu |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with |
||||
* the License. You may obtain a copy of the License in the LICENSE file, or at: |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package ro.fortsoft.pf4j; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.lang.reflect.Constructor; |
||||
import java.lang.reflect.Modifier; |
||||
|
||||
/** |
||||
* The default implementation for PluginFactory. |
||||
* It uses Class.newInstance() method. |
||||
* |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class DefaultPluginFactory implements PluginFactory { |
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DefaultExtensionFactory.class); |
||||
|
||||
/** |
||||
* Creates a plugin instance. If an error occurs than that error is logged and the method returns null. |
||||
* @param pluginWrapper |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public Plugin create(final PluginWrapper pluginWrapper) { |
||||
String pluginClassName = pluginWrapper.getDescriptor().getPluginClass(); |
||||
log.debug("Create instance for plugin '{}'", pluginClassName); |
||||
|
||||
Class<?> pluginClass; |
||||
try { |
||||
pluginClass = pluginWrapper.getPluginClassLoader().loadClass(pluginClassName); |
||||
} catch (ClassNotFoundException e) { |
||||
log.error(e.getMessage(), e); |
||||
return null; |
||||
} |
||||
|
||||
// once we have the class, we can do some checks on it to ensure
|
||||
// that it is a valid implementation of a plugin.
|
||||
int modifiers = pluginClass.getModifiers(); |
||||
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) |
||||
|| (!Plugin.class.isAssignableFrom(pluginClass))) { |
||||
log.error("The plugin class '{}' is not valid", pluginClassName); |
||||
return null; |
||||
} |
||||
|
||||
// create the plugin instance
|
||||
try { |
||||
Constructor<?> constructor = pluginClass.getConstructor(new Class[] { PluginWrapper.class }); |
||||
return (Plugin) constructor.newInstance(new Object[] { pluginWrapper }); |
||||
} catch (Exception e) { |
||||
log.error(e.getMessage(), e); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,39 @@
|
||||
/* |
||||
* Copyright 2014 Decebal Suiu |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with |
||||
* the License. You may obtain a copy of the License in the LICENSE file, or at: |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package ro.fortsoft.pf4j; |
||||
|
||||
/** |
||||
* @author Decebal Suiu |
||||
*/ |
||||
public class ExtensionDescriptor { |
||||
|
||||
private int ordinal; |
||||
private Class<?> extensionClass; |
||||
|
||||
public Class<?> getExtensionClass() { |
||||
return extensionClass; |
||||
} |
||||
|
||||
public int getOrdinal() { |
||||
return ordinal; |
||||
} |
||||
|
||||
void setExtensionClass(Class<?> extensionClass) { |
||||
this.extensionClass = extensionClass; |
||||
} |
||||
|
||||
void setOrdinal(int ordinal) { |
||||
this.ordinal = ordinal; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,22 @@
|
||||
/* |
||||
* Copyright 2014 Decebal Suiu |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with |
||||
* the License. You may obtain a copy of the License in the LICENSE file, or at: |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package ro.fortsoft.pf4j; |
||||
|
||||
/** |
||||
* Creates an extension instance. |
||||
*/ |
||||
public interface ExtensionFactory { |
||||
|
||||
public Object create(Class<?> extensionClass); |
||||
|
||||
} |
@ -0,0 +1,22 @@
|
||||
/* |
||||
* Copyright 2014 Decebal Suiu |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with |
||||
* the License. You may obtain a copy of the License in the LICENSE file, or at: |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package ro.fortsoft.pf4j; |
||||
|
||||
/** |
||||
* Creates a plugin instance. |
||||
*/ |
||||
public interface PluginFactory { |
||||
|
||||
public Plugin create(PluginWrapper pluginWrapper); |
||||
|
||||
} |
Loading…
Reference in new issue