From 8ad8be4bf3a16f43d860606454ee382dd8502819 Mon Sep 17 00:00:00 2001
From: Jean-Noël Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 11 May 2016 16:24:26 +0000
Subject: [PATCH] OPENDJ-2932 Example plugin is incomplete

---
 /dev/null                                                                                  |   47 -----------
 opendj-server-example-plugin/src/main/assembly/descriptor.xml                              |   36 ++++++--
 opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin_fr.properties |    8 +-
 opendj-server-example-plugin/src/main/java/com/example/opendj/ExamplePlugin.java           |   83 ++++++++++++++++----
 opendj-server-example-plugin/README.example.plugin                                         |   38 +++++++++
 opendj-server-example-plugin/pom.xml                                                       |   18 +--
 opendj-server-example-plugin/src/main/assembly/config/example-plugin.ldif                  |    2 
 opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin.properties    |    8 +-
 8 files changed, 143 insertions(+), 97 deletions(-)

diff --git a/opendj-server-example-plugin/README b/opendj-server-example-plugin/README
deleted file mode 100644
index 8d2a2b2..0000000
--- a/opendj-server-example-plugin/README
+++ /dev/null
@@ -1,47 +0,0 @@
-This folder contains source code for an example "Hello World" style
-plugin. It features a plugin which has a configurable message (the
-default being "Hello World") which is displayed as a notice message
-when OpenDJ directory server is started.
-
-In order to build and use this example plugin, perform the following
-steps while the server is stopped:
-
-  1. Then unzip the example-plugin.zip (in place):
-
-     unzip example-plugin.zip
-
-  2. Go into the example-plugin source folder:
-
-     cd example-plugin
-
-  3. And build the plugin (this requires Ant version 7 and Xalan-Java):
-
-     ant install
-
-     If this step fails for you as is, you can find Ant 7 and Xalan-Java
-     in the OpenDJ ext/ directory after checking the source out with
-     Subversion. Substitute your full path to OpenDJ sources for /src
-     in the following command:
-
-     /src/ext/ant/bin/ant -Dxalan.directory=/src/ext/xalan-j/ install
-
-  4. This will copy the following files into the parent OpenDJ
-     installation:
-
-     lib/extensions/example-plugin.jar
-     config/example-plugin.ldif
-     config/schema/99-example-plugin.ldif
-
-  5. Add the plugin's config to the server configuration.
-
-     cd ../bin
-     ./start-ds
-     ./dsconfig -h `hostname` -p 4444 -D "cn=Directory Manager" -w password
-      create-plugin --plugin-name "Example Plugin" --type example
-      --set enabled:true --set plugin-type:startup
-      --set java-class:com.example.opends.ExamplePlugin -X -n
-
-  6. Restart the server and look for the "hello world" notice in the start
-     up log:
-
-     ./stop-ds --restart
diff --git a/opendj-server-example-plugin/README.example.plugin b/opendj-server-example-plugin/README.example.plugin
new file mode 100644
index 0000000..39e3a2c
--- /dev/null
+++ b/opendj-server-example-plugin/README.example.plugin
@@ -0,0 +1,38 @@
+This folder contains source code for an example "Hello World" style
+plugin. It features a plugin which has a configurable message (the
+default being "Hello World") which is displayed as a notice message
+when OpenDJ directory server is started.
+
+In order to build and use this example plugin, perform the following
+steps while the server is stopped:
+
+  1. Then unzip the example-plugin.zip (in place):
+
+     unzip example-plugin.zip
+
+  2. Go into the example-plugin source folder:
+
+     cd example-plugin
+
+  3. And build the plugin (this requires Maven version 3):
+
+     mvn clean install
+
+  4. This will copy the following files into the parent OpenDJ installation:
+
+     lib/extensions/example-plugin.jar
+     config/example-plugin.ldif
+     config/schema/99-example-plugin.ldif
+
+  5. Add the plugin's config to the server configuration.
+
+     cd ../bin
+     ./start-ds
+     ./dsconfig -h `hostname` -p 4444 -D "cn=Directory Manager" -w password
+      create-plugin --plugin-name "Example Plugin" --type example
+      --set enabled:true --set plugin-type:startup -X -n
+
+  6. Restart the server and look for the "HELLO WORLD" notice in the start
+     up log:
+
+     ./stop-ds --restart
diff --git a/opendj-server-example-plugin/pom.xml b/opendj-server-example-plugin/pom.xml
index 4a1e42e..0354891 100644
--- a/opendj-server-example-plugin/pom.xml
+++ b/opendj-server-example-plugin/pom.xml
@@ -30,19 +30,15 @@
   <dependencies>
     <dependency>
       <groupId>org.forgerock.opendj</groupId>
-      <artifactId>opendj-core</artifactId>
+      <artifactId>opendj-server-legacy</artifactId>
+      <version>${project.version}</version>
     </dependency>
+    <!-- This adds an external dependency for the plugin.
+         Also update src/main/assembly/descriptor.xml file to include the dependency -->
     <dependency>
-      <groupId>org.forgerock.opendj</groupId>
-      <artifactId>opendj-config</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.forgerock.commons</groupId>
-      <artifactId>i18n-slf4j</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-api</artifactId>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.3.2</version>
     </dependency>
   </dependencies>
   <build>
diff --git a/opendj-server-example-plugin/src/main/assembly/config/example-plugin.ldif b/opendj-server-example-plugin/src/main/assembly/config/example-plugin.ldif
index 04d5645..4e9c2a5 100644
--- a/opendj-server-example-plugin/src/main/assembly/config/example-plugin.ldif
+++ b/opendj-server-example-plugin/src/main/assembly/config/example-plugin.ldif
@@ -4,7 +4,7 @@
 objectClass: ds-cfg-example-plugin
 cn: Example Plugin
 ds-cfg-enabled: true
-ds-cfg-java-class: com.example.opends.ExamplePlugin
+ds-cfg-java-class: com.example.opendj.ExamplePlugin
 ds-cfg-plugin-type: startup
 ds-cfg-example-plugin-message: Hello World
 
diff --git a/opendj-server-example-plugin/src/main/assembly/descriptor.xml b/opendj-server-example-plugin/src/main/assembly/descriptor.xml
index 43ba652..3f6c0fe 100644
--- a/opendj-server-example-plugin/src/main/assembly/descriptor.xml
+++ b/opendj-server-example-plugin/src/main/assembly/descriptor.xml
@@ -12,7 +12,7 @@
   Header, with the fields enclosed by brackets [] replaced by your own identifying
   information: "Portions Copyright [year] [name of copyright owner]".
 
-  Copyright 2014 ForgeRock AS.
+  Copyright 2014-2016 ForgeRock AS.
  -->
 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -29,7 +29,7 @@
       <directoryMode>755</directoryMode>
       <fileMode>644</fileMode>
       <includes>
-        <include>README</include>
+        <include>README.example.plugin</include>
         <include>LICENSE</include>
         <include>NOTICE</include>
       </includes>
@@ -40,15 +40,15 @@
       <directoryMode>0755</directoryMode>
       <fileMode>0644</fileMode>
     </fileSet>
-  <fileSet>
-    <directory>${project.parent.parent.basedir}</directory>
-    <outputDirectory></outputDirectory>
-    <directoryMode>755</directoryMode>
-    <fileMode>644</fileMode>
-    <includes>
-      <include>*.png</include>
-    </includes>
-  </fileSet>
+    <fileSet>
+      <directory>${project.parent.parent.basedir}</directory>
+      <outputDirectory></outputDirectory>
+      <directoryMode>755</directoryMode>
+      <fileMode>644</fileMode>
+      <includes>
+        <include>*.png</include>
+      </includes>
+    </fileSet>
     <fileSet>
       <directory>src/main/assembly/config</directory>
       <outputDirectory>config</outputDirectory>
@@ -62,6 +62,20 @@
       <includes>
         <include>*.jar</include>
       </includes>
+      <excludes>
+        <exclude>*-sources.jar</exclude>
+      </excludes>
     </fileSet>
   </fileSets>
+  <dependencySets>
+    <dependencySet>
+      <outputDirectory>lib/extensions</outputDirectory>
+      <includes>
+        <include>org.apache.commons:commons-lang3</include>
+      </includes>
+      <useStrictFiltering>true</useStrictFiltering>
+      <useProjectArtifact>true</useProjectArtifact>
+      <useTransitiveDependencies>true</useTransitiveDependencies>
+    </dependencySet>
+  </dependencySets>
 </assembly>
diff --git a/opendj-server-example-plugin/src/main/java/com/example/opendj/ExamplePlugin.java b/opendj-server-example-plugin/src/main/java/com/example/opendj/ExamplePlugin.java
index 5207651..27976d3 100644
--- a/opendj-server-example-plugin/src/main/java/com/example/opendj/ExamplePlugin.java
+++ b/opendj-server-example-plugin/src/main/java/com/example/opendj/ExamplePlugin.java
@@ -12,49 +12,94 @@
  * information: "Portions Copyright [year] [name of copyright owner]".
  *
  * Copyright 2006-2008 Sun Microsystems, Inc.
- * Portions copyright 2014-2015 ForgeRock AS.
+ * Portions copyright 2014-2016 ForgeRock AS.
  */
 package com.example.opendj;
 
-import java.util.List;
+import static com.example.opendj.ExamplePluginMessages.*;
 
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.lang3.StringUtils;
 import org.forgerock.i18n.LocalizableMessage;
+import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.server.ConfigChangeResult;
+import org.forgerock.opendj.config.server.ConfigException;
 import org.forgerock.opendj.config.server.ConfigurationChangeListener;
+import org.opends.server.api.plugin.DirectoryServerPlugin;
+import org.opends.server.api.plugin.PluginResult;
+import org.opends.server.api.plugin.PluginType;
+import org.opends.server.types.InitializationException;
 
 import com.example.opendj.server.ExamplePluginCfg;
 
 /**
- * The example plugin implementation class. This plugin will output the
- * configured message to the error log during server start up.
+ * The example plugin implementation class. This plugin will output the configured message to the
+ * error log during server start up.
  */
-public class ExamplePlugin implements ConfigurationChangeListener<ExamplePluginCfg> {
-    // FIXME: fill in the remainder of this class once the server plugin API is migrated.
+public class ExamplePlugin extends DirectoryServerPlugin<ExamplePluginCfg>
+                           implements ConfigurationChangeListener<ExamplePluginCfg> {
+    private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
 
-    /**
-     * Default constructor.
-     */
+    /** The current configuration. */
+    private ExamplePluginCfg config;
+
+    /** Default constructor. */
     public ExamplePlugin() {
-        // No implementation required.
+        super();
     }
 
-    /** {@inheritDoc} */
     @Override
-    public ConfigChangeResult applyConfigurationChange(final ExamplePluginCfg config) {
+    public void initializePlugin(Set<PluginType> pluginTypes, ExamplePluginCfg configuration)
+            throws ConfigException, InitializationException {
+        // This plugin may only be used as a server startup plugin.
+        for (PluginType t : pluginTypes) {
+            switch (t) {
+            case STARTUP:
+                // This is fine.
+                break;
+            default:
+                throw new ConfigException(ERR_INITIALIZE_PLUGIN.get(String.valueOf(t)));
+            }
+        }
+
+        // Register change listeners. These are not really necessary for this plugin
+        // since it is only used during server start-up.
+        configuration.addExampleChangeListener(this);
+
+        // Save the configuration.
+        this.config = configuration;
+    }
+
+    @Override
+    public PluginResult.Startup doStartup() {
+        // Log the provided message.
+        logger.info(NOTE_DO_STARTUP, StringUtils.upperCase(config.getMessage()));
+        return PluginResult.Startup.continueStartup();
+    }
+
+    @Override
+    public ConfigChangeResult applyConfigurationChange(ExamplePluginCfg config) {
         // The new configuration has already been validated.
 
+        // Log a message to say that the configuration has changed.
+        // This is not necessary, but we'll do it just to show that the change
+        // has taken effect.
+        logger.info(NOTE_APPLY_CONFIGURATION_CHANGE, this.config.getMessage(), config.getMessage());
+
+        // Update the configuration.
+        this.config = config;
+
         // Update was successful, no restart required.
         return new ConfigChangeResult();
     }
 
-    /** {@inheritDoc} */
     @Override
-    public boolean isConfigurationChangeAcceptable(final ExamplePluginCfg config,
-            final List<LocalizableMessage> messages) {
-        /*
-         * The only thing that can be validated here is the plugin's message.
-         * However, it is always going to be valid, so let's always return true.
-         */
+    public boolean isConfigurationChangeAcceptable(ExamplePluginCfg config, List<LocalizableMessage> messages) {
+        // The only thing that can be validated here is the plugin's message.
+        // However, it is always going to be valid, so let's always return true.
         return true;
     }
 }
+
diff --git a/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin.properties b/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin.properties
index 85860ce..f8d98d5 100644
--- a/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin.properties
+++ b/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin.properties
@@ -11,8 +11,8 @@
 # information: "Portions Copyright [year] [name of copyright owner]".
 #
 # Copyright 2006-2008 Sun Microsystems, Inc.
-# Portions copyright 2014 ForgeRock AS.
-SEVERE_ERR_INITIALIZE_PLUGIN_1="Invalid plugin type %s for the example plugin.
-NOTICE_DO_STARTUP_2=Example plugin message '%s'.
-NOTICE_APPLY_CONFIGURATION_CHANGE_3="Example plugin message has been changed \
+# Portions copyright 2014-2016 ForgeRock AS.
+ERR_INITIALIZE_PLUGIN_1="Invalid plugin type %s for the example plugin.
+NOTE_DO_STARTUP_2=Example plugin message '%s'.
+NOTE_APPLY_CONFIGURATION_CHANGE_3="Example plugin message has been changed \
 from '%s' to '%s'.
diff --git a/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin_fr.properties b/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin_fr.properties
index 60876e6..ab4455b 100644
--- a/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin_fr.properties
+++ b/opendj-server-example-plugin/src/main/java/com/example/opendj/example_plugin_fr.properties
@@ -11,8 +11,8 @@
 # information: "Portions Copyright [year] [name of copyright owner]".
 #
 # Copyright 2006-2008 Sun Microsystems, Inc.
-# Portions copyright 2014 ForgeRock AS.
-SEVERE_ERR_INITIALIZE_PLUGIN_1="Type de plugin invalide %s pour le plugin d'exemple.
-NOTICE_DO_STARTUP_2=Message du plugin d'exemple '%s'.
-NOTICE_APPLY_CONFIGURATION_CHANGE_3="Le message du plugin d'exemple a été modifié \
+# Portions copyright 2014-2016 ForgeRock AS.
+ERR_INITIALIZE_PLUGIN_1="Type de plugin invalide %s pour le plugin d'exemple.
+NOTE_DO_STARTUP_2=Message du plugin d'exemple '%s'.
+NOTE_APPLY_CONFIGURATION_CHANGE_3="Le message du plugin d'exemple a été modifié \
 de '%s' en '%s'.

--
Gitblit v1.10.0