File: websiteExample

package info (click to toggle)
libpf4j-java 3.12.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,544 kB
  • sloc: java: 8,516; xml: 1,096; sh: 228; makefile: 2
file content (131 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh
# autopkgtest check: Run the example of the website, also in demo/
# (C) 2012-present the original author or authors.
#     2021 Pierre Gruet.
# Author: Pierre Gruet <pgt@debian.org>

set -e

WORKDIR=$(mktemp -d)
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR

# Creating the interface marking an extension point.
cat << EOF >Greeting.java
import org.pf4j.ExtensionPoint;

public interface Greeting extends ExtensionPoint {

    String getGreeting();

}
EOF

# Creating the extension class.
cat <<EOF >WelcomeGreeting.java
import org.pf4j.Extension;
import org.pf4j.ExtensionPoint;

@Extension
public class WelcomeGreeting implements Greeting {

    @Override
    public String getGreeting() {
        return "Welcome";
    }

}
EOF

# Creating a class extending Plugin to manage startup or stopping phases.
cat <<EOF >WelcomePlugin.java
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

public class WelcomePlugin extends Plugin {

    public WelcomePlugin(PluginWrapper wrapper) {
        super(wrapper);

        // you can use "wrapper" to have access to the plugin context (plugin manager, descriptor, ...)
    }

    @Override
    public void start() {
        System.out.println("WelcomePlugin.start()");
    }

    @Override
    public void stop() {
        System.out.println("WelcomePlugin.stop()");
    }
    
    @Override
    public void delete() {
        System.out.println("WelcomePlugin.delete()");
    }
}
EOF

# Creating the manifest, indicating the plugin class, the id and the version
# number.
mkdir META-INF/

cat <<EOF >META-INF/MANIFEST.MF
Manifest-Version: 1.0
Plugin-Class: WelcomePlugin
Plugin-Id: welcome-plugin
Plugin-Version: 0.0.1
EOF

# Compiling classes, then adding extensions.idx (created by pf4j) into
# META-INF/, then building the plugin jar and cleaning up.
javac -cp /usr/share/java/pf4j.jar Greeting.java WelcomeGreeting.java WelcomePlugin.java
mv extensions.idx META-INF/
zip -q -r plugin.jar META-INF/ Greeting.class WelcomeGreeting.class WelcomePlugin.class
rm -rf *.java *.class META-INF/

# Class with the test program.
cat <<EOF >Test.java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;

public class Test {

    public static void main(String[] args) {
        // create the plugin manager
        Path[] pluginsDir = new Path[1];
        pluginsDir[0] = Paths.get(".");
        PluginManager pluginManager = new JarPluginManager(pluginsDir);

        // start and load all plugins of application
        pluginManager.loadPlugins();
        pluginManager.startPlugins();

        // retrieve all extensions for "Greeting" extension point
        List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
        for (Greeting greeting : greetings) {
            System.out.println(">>> " + greeting.getGreeting());
        }

        // stop and unload all plugins
        pluginManager.stopPlugins();
        pluginManager.unloadPlugins();
    }
}
EOF

# Compiling this class, before running it.
javac -cp /usr/share/java/pf4j.jar:plugin.jar Test.java

# See http://www.slf4j.org/codes.html#StaticLoggerBinder to understand why
# slf4j-simple.jar is included in this classpath.
java -cp /usr/share/java/pf4j.jar:/usr/share/java/slf4j-simple.jar:plugin.jar:. Test | grep -q ">>> Welcome"

if [ $? -ne 0 ]; then
  exit 1
fi