Merge remote-tracking branch 'upstream/master'

This commit is contained in:
gschambial
2017-11-13 10:46:30 +05:30
280 changed files with 1749 additions and 41 deletions
+7
View File
@@ -216,6 +216,13 @@
<artifactId>spring-web</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
@@ -0,0 +1,9 @@
package com.baeldung.javac;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.PARAMETER})
public @interface Positive {
}
@@ -0,0 +1,123 @@
package com.baeldung.javac;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.*;
import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import javax.tools.JavaCompiler;
import java.util.*;
import java.util.stream.Collectors;
import static com.sun.tools.javac.util.List.nil;
/**
* A {@link JavaCompiler javac} plugin which inserts {@code >= 0} checks into resulting {@code *.class} files
* for numeric method parameters marked by {@link Positive}
*/
public class SampleJavacPlugin implements Plugin {
public static final String NAME = "MyPlugin";
private static Set<String> TARGET_TYPES = new HashSet<>(Arrays.asList(
// Use only primitive types for simplicity
byte.class.getName(), short.class.getName(), char.class.getName(),
int.class.getName(), long.class.getName(), float.class.getName(), double.class.getName()));
@Override
public String getName() {
return NAME;
}
@Override
public void init(JavacTask task, String... args) {
Context context = ((BasicJavacTask) task).getContext();
task.addTaskListener(new TaskListener() {
@Override
public void started(TaskEvent e) {
}
@Override
public void finished(TaskEvent e) {
if (e.getKind() != TaskEvent.Kind.PARSE) {
return;
}
e.getCompilationUnit()
.accept(new TreeScanner<Void, Void>() {
@Override
public Void visitMethod(MethodTree method, Void v) {
List<VariableTree> parametersToInstrument = method.getParameters()
.stream()
.filter(SampleJavacPlugin.this::shouldInstrument)
.collect(Collectors.toList());
if (!parametersToInstrument.isEmpty()) {
// There is a possible case that more than one argument is marked by @Positive,
// as the checks are added to the method's body beginning, we process parameters RTL
// to ensure correct order.
Collections.reverse(parametersToInstrument);
parametersToInstrument.forEach(p -> addCheck(method, p, context));
}
// There is a possible case that there is a nested class declared in a method's body,
// hence, we want to proceed with method body AST as well.
return super.visitMethod(method, v);
}
}, null);
}
});
}
private boolean shouldInstrument(VariableTree parameter) {
return TARGET_TYPES.contains(parameter.getType().toString())
&& parameter.getModifiers().getAnnotations()
.stream()
.anyMatch(a -> Positive.class.getSimpleName().equals(a.getAnnotationType().toString()));
}
private void addCheck(MethodTree method, VariableTree parameter, Context context) {
JCTree.JCIf check = createCheck(parameter, context);
JCTree.JCBlock body = (JCTree.JCBlock) method.getBody();
body.stats = body.stats.prepend(check);
}
private static JCTree.JCIf createCheck(VariableTree parameter, Context context) {
TreeMaker factory = TreeMaker.instance(context);
Names symbolsTable = Names.instance(context);
return factory.at(((JCTree) parameter).pos)
.If(factory.Parens(createIfCondition(factory, symbolsTable, parameter)),
createIfBlock(factory, symbolsTable, parameter),
null);
}
private static JCTree.JCBinary createIfCondition(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
Name parameterId = symbolsTable.fromString(parameter.getName().toString());
return factory.Binary(JCTree.Tag.LE,
factory.Ident(parameterId),
factory.Literal(TypeTag.INT, 0));
}
private static JCTree.JCBlock createIfBlock(TreeMaker factory, Names symbolsTable, VariableTree parameter) {
String parameterName = parameter.getName().toString();
Name parameterId = symbolsTable.fromString(parameterName);
String errorMessagePrefix = String.format("Argument '%s' of type %s is marked by @%s but got '",
parameterName, parameter.getType(), Positive.class.getSimpleName());
String errorMessageSuffix = "' for it";
return factory.Block(0, com.sun.tools.javac.util.List.of(
factory.Throw(
factory.NewClass(null, nil(),
factory.Ident(symbolsTable.fromString(IllegalArgumentException.class.getSimpleName())),
com.sun.tools.javac.util.List.of(factory.Binary(JCTree.Tag.PLUS,
factory.Binary(JCTree.Tag.PLUS, factory.Literal(TypeTag.CLASS, errorMessagePrefix),
factory.Ident(parameterId)),
factory.Literal(TypeTag.CLASS, errorMessageSuffix))), null))));
}
}
@@ -0,0 +1 @@
com.baeldung.javac.SampleJavacPlugin
@@ -0,0 +1,42 @@
package com.baeldung.javac;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SampleJavacPluginIntegrationTest {
private static final String CLASS_TEMPLATE =
"package com.baeldung.javac;\n" +
"\n" +
"public class Test {\n" +
" public static %1$s service(@Positive %1$s i) {\n" +
" return i;\n" +
" }\n" +
"}\n" +
"";
private TestCompiler compiler = new TestCompiler();
private TestRunner runner = new TestRunner();
@Test(expected = IllegalArgumentException.class)
public void givenInt_whenNegative_thenThrowsException() throws Throwable {
compileAndRun(double.class,-1);
}
@Test(expected = IllegalArgumentException.class)
public void givenInt_whenZero_thenThrowsException() throws Throwable {
compileAndRun(int.class,0);
}
@Test
public void givenInt_whenPositive_thenSuccess() throws Throwable {
assertEquals(1, compileAndRun(int.class, 1));
}
private Object compileAndRun(Class<?> argumentType, Object argument) throws Throwable {
String qualifiedClassName = "com.baeldung.javac.Test";
byte[] byteCode = compiler.compile(qualifiedClassName, String.format(CLASS_TEMPLATE, argumentType.getName()));
return runner.run(byteCode, qualifiedClassName, "service", new Class[] {argumentType}, argument);
}
}
@@ -0,0 +1,26 @@
package com.baeldung.javac;
import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
/** Holds compiled byte code in a byte array */
public class SimpleClassFile extends SimpleJavaFileObject {
private ByteArrayOutputStream out;
public SimpleClassFile(URI uri) {
super(uri, Kind.CLASS);
}
@Override
public OutputStream openOutputStream() throws IOException {
return out = new ByteArrayOutputStream();
}
public byte[] getCompiledBinaries() {
return out.toByteArray();
}
}
@@ -0,0 +1,34 @@
package com.baeldung.javac;
import javax.tools.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
/** Adapts {@link SimpleClassFile} to the {@link JavaCompiler} */
public class SimpleFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
private final List<SimpleClassFile> compiled = new ArrayList<>();
public SimpleFileManager(StandardJavaFileManager delegate) {
super(delegate);
}
@Override
public JavaFileObject getJavaFileForOutput(Location location,
String className,
JavaFileObject.Kind kind,
FileObject sibling)
{
SimpleClassFile result = new SimpleClassFile(URI.create("string://" + className));
compiled.add(result);
return result;
}
/**
* @return compiled binaries processed by the current class
*/
public List<SimpleClassFile> getCompiled() {
return compiled;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.javac;
import javax.tools.SimpleJavaFileObject;
import java.net.URI;
/** Exposes given test source to the compiler. */
public class SimpleSourceFile extends SimpleJavaFileObject {
private final String content;
public SimpleSourceFile(String qualifiedClassName, String testSource) {
super(URI.create(String.format("file://%s%s",
qualifiedClassName.replaceAll("\\.", "/"),
Kind.SOURCE.extension)),
Kind.SOURCE);
content = testSource;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return content;
}
}
@@ -0,0 +1,35 @@
package com.baeldung.javac;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
public class TestCompiler {
public byte[] compile(String qualifiedClassName, String testSource) {
StringWriter output = new StringWriter();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
SimpleFileManager fileManager = new SimpleFileManager(compiler.getStandardFileManager(
null,
null,
null
));
List<SimpleSourceFile> compilationUnits = singletonList(new SimpleSourceFile(qualifiedClassName, testSource));
List<String> arguments = new ArrayList<>();
arguments.addAll(asList("-classpath", System.getProperty("java.class.path"),
"-Xplugin:" + SampleJavacPlugin.NAME));
JavaCompiler.CompilationTask task = compiler.getTask(output,
fileManager,
null,
arguments,
null,
compilationUnits);
task.call();
return fileManager.getCompiled().iterator().next().getCompiledBinaries();
}
}
@@ -0,0 +1,41 @@
package com.baeldung.javac;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestRunner {
public Object run(byte[] byteCode,
String qualifiedClassName,
String methodName,
Class<?>[] argumentTypes,
Object... args)
throws Throwable
{
ClassLoader classLoader = new ClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
return defineClass(name, byteCode, 0, byteCode.length);
}
};
Class<?> clazz;
try {
clazz = classLoader.loadClass(qualifiedClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't load compiled test class", e);
}
Method method;
try {
method = clazz.getMethod(methodName, argumentTypes);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Can't find the 'main()' method in the compiled test class", e);
}
try {
return method.invoke(null, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
@@ -0,0 +1,141 @@
package com.baeldung.string.formatter;
import java.util.Calendar;
import java.util.Formatter;
import java.util.GregorianCalendar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class StringFormatterExampleTests {
@Test
public void givenString_whenFormatSpecifierForCalendar_thenGotExpected() {
//Syntax of Format Specifiers for Date/Time Representation
Calendar c = new GregorianCalendar(2017, 11, 10);
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
assertEquals("The date is: 12 10,2017", s);
}
@Test
public void givenString_whenGeneralConversion_thenConvertedString() {
//General Conversions
String s = String.format("The correct answer is %s", false);
assertEquals("The correct answer is false", s);
s = String.format("The correct answer is %b", null);
assertEquals("The correct answer is false", s);
s = String.format("The correct answer is %B", true);
assertEquals("The correct answer is TRUE", s);
}
@Test
public void givenString_whenCharConversion_thenConvertedString() {
//Character Conversions
String s = String.format("The correct answer is %c", 'a');
assertEquals("The correct answer is a", s);
s = String.format("The correct answer is %c", null);
assertEquals("The correct answer is null", s);
s = String.format("The correct answer is %C", 'b');
assertEquals("The correct answer is B", s);
s = String.format("The valid unicode character: %c", 0x0400);
assertTrue(Character.isValidCodePoint(0x0400));
assertEquals("The valid unicode character: Ѐ", s);
}
@Test(expected = java.util.IllegalFormatCodePointException.class)
public void givenString_whenIllegalCodePointForConversion_thenError() {
String s = String.format("The valid unicode character: %c", 0x11FFFF);
assertFalse(Character.isValidCodePoint(0x11FFFF));
assertEquals("The valid unicode character: Ā", s);
}
@Test
public void givenString_whenNumericIntegralConversion_thenConvertedString() {
//Numeric Integral Conversions
String s = String.format("The number 25 in decimal = %d", 25);
assertEquals("The number 25 in decimal = 25", s);
s = String.format("The number 25 in octal = %o", 25);
assertEquals("The number 25 in octal = 31", s);
s = String.format("The number 25 in hexadecimal = %x", 25);
assertEquals("The number 25 in hexadecimal = 19", s);
}
@Test
public void givenString_whenNumericFloatingConversion_thenConvertedString() {
//Numeric Floating-point Conversions
String s = String.format("The computerized scientific format of 10000.00 "
+ "= %e", 10000.00);
assertEquals("The computerized scientific format of 10000.00 = 1.000000e+04", s);
s = String.format("The decimal format of 10.019 = %f", 10.019);
assertEquals("The decimal format of 10.019 = 10.019000", s);
}
@Test
public void givenString_whenLineSeparatorConversion_thenConvertedString() {
//Line Separator Conversion
String s = String.format("First Line %nSecond Line");
assertEquals("First Line \n"
+ "Second Line", s);
}
@Test
public void givenString_whenSpecifyFlag_thenGotFormattedString() {
//Without left-justified flag
String s = String.format("Without left justified flag: %5d", 25);
assertEquals("Without left justified flag: 25", s);
//Using left-justified flag
s = String.format("With left justified flag: %-5d", 25);
assertEquals("With left justified flag: 25 ", s);
}
@Test
public void givenString_whenSpecifyPrecision_thenGotExpected() {
//Precision
String s = String.format("Output of 25.09878 with Precision 2: %.2f", 25.09878);
assertEquals("Output of 25.09878 with Precision 2: 25.10", s);
s = String.format("Output of general conversion type with Precision 2: %.2b", true);
assertEquals("Output of general conversion type with Precision 2: tr", s);
}
@Test
public void givenString_whenSpecifyArgumentIndex_thenGotExpected() {
Calendar c = new GregorianCalendar(2017, 11, 10);
//Argument_Index
String s = String.format("The date is: %1$tm %1$te,%1$tY", c);
assertEquals("The date is: 12 10,2017", s);
s = String.format("The date is: %1$tm %<te,%<tY", c);
assertEquals("The date is: 12 10,2017", s);
}
@Test
public void givenAppendable_whenCreateFormatter_thenFormatterWorksOnAppendable() {
//Using String Formatter with Appendable
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter.format("I am writting to a %1$s Instance.", sb.getClass());
assertEquals("I am writting to a class java.lang.StringBuilder Instance.", sb.toString());
}
@Test
public void givenString_whenNoArguments_thenExpected() {
//Using String Formatter without arguments
String s = String.format("John scored 90%% in Fall semester");
assertEquals("John scored 90% in Fall semester", s);
}
}
@@ -16,32 +16,29 @@ import wildfly.beans.UserBeanLocal;
* Servlet implementation class TestEJBServlet
*/
public class TestEJBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private UserBeanLocal userBean;
@EJB
private UserBeanLocal userBean;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<User> users = userBean.getUsers();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = userBean.getUsers();
PrintWriter out = response.getWriter();
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Users</title></head>");
out.println("<body>");
out.println("<center><h1>List of users:</h1>");
out.println("<table border=\"1\" align=\"center\" style=\"width:50%\">");
for (User user : users) {
out.println("<tr>");
out.print("<td>" + user.getUsername() + "</td>");
out.print("<td>" + user.getEmail() + "</td>");
out.println("</tr>");
}
}
out.println("<html>");
out.println("<head><title>Users</title></head>");
out.println("<body>");
out.println("<center><h1>List of users:</h1>");
out.println("<table border=\"1\" align=\"center\" style=\"width:50%\">");
for (User user : users) {
out.println("<tr>");
out.print("<td>" + user.getUsername() + "</td>");
out.print("<td>" + user.getEmail() + "</td>");
out.println("</tr>");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
+1
View File
@@ -1,3 +1,4 @@
## Relevant articles:
- [Dynamic Mapping with Hibernate](http://www.baeldung.com/hibernate-dynamic-mapping)
- [An Overview of Identifiers in Hibernate](http://www.baeldung.com/hibernate-identifiers)
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<artifactId>osgi-intro-sample-activator</artifactId>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- Qualified name of the class that exposes the activator iface. -->
<Bundle-Activator>com.baeldung.osgi.sample.activator.HelloWorld</Bundle-Activator>
<!-- One important thing to note:
since you are not exporting the package "com.baeldung.osgi.sample.activator",
you should at least add it to the Private-Package instruction.
Otherwise, the classes inside the package will not be copied to your bundle,
as the default value of this instruction is empty. -->
<Private-Package>com.baeldung.osgi.sample.activator</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package com.baeldung.osgi.sample.activator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class HelloWorld implements BundleActivator {
public void start(BundleContext ctx) {
System.out.println("Hello World.");
}
public void stop(BundleContext bundleContext) {
System.out.println("Goodbye World.");
}
}
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
<artifactId>osgi-intro-sample-client</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>osgi-intro-sample-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.client.Client</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.client</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,44 @@
package com.baeldung.osgi.sample.client;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.*;
public class Client implements BundleActivator, ServiceListener {
private BundleContext ctx;
private ServiceReference serviceReference;
public void start(BundleContext ctx) {
this.ctx = ctx;
try {
ctx.addServiceListener(this, "(objectclass=" + Greeter.class.getName() + ")");
} catch (InvalidSyntaxException ise) {
ise.printStackTrace();
}
}
public void stop(BundleContext bundleContext) {
if (serviceReference != null) {
ctx.ungetService(serviceReference);
}
this.ctx = null;
}
public void serviceChanged(ServiceEvent serviceEvent) {
int type = serviceEvent.getType();
switch (type) {
case (ServiceEvent.REGISTERED):
System.out.println("Notification of service registered.");
serviceReference = serviceEvent.getServiceReference();
Greeter service = (Greeter) (ctx.getService(serviceReference));
System.out.println(service.sayHiTo("John"));
break;
case (ServiceEvent.UNREGISTERING):
System.out.println("Notification of service unregistered.");
ctx.ungetService(serviceEvent.getServiceReference());
break;
default:
break;
}
}
}
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>osgi-intro-sample-service</artifactId>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.service.implementation.GreeterImpl</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.service.implementation</Private-Package>
<Export-Package>com.baeldung.osgi.sample.service.definition</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.osgi.sample.service.definition;
public interface Greeter {
public String sayHiTo(String name);
}
@@ -0,0 +1,30 @@
package com.baeldung.osgi.sample.service.implementation;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import java.util.Hashtable;
public class GreeterImpl implements Greeter, BundleActivator {
private ServiceReference<Greeter> reference;
private ServiceRegistration<Greeter> registration;
@Override public String sayHiTo(String name) {
return "Hello " + name;
}
@Override public void start(BundleContext context) throws Exception {
System.out.println("Registering service.");
registration = context.registerService(Greeter.class, new GreeterImpl(), new Hashtable<String, String>());
reference = registration.getReference();
}
@Override public void stop(BundleContext context) throws Exception {
System.out.println("Unregistering service.");
registration.unregister();
}
}
+87
View File
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>osgi-intro</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>osgi-intro-sample-activator</module>
<module>osgi-intro-sample-service</module>
<module>osgi-intro-sample-client</module>
</modules>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-gxyz</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-mapquest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
+91
View File
@@ -0,0 +1,91 @@
OSGi
====
Info
---
com.baeldung.osgi
com.baeldung.osgi.sample.activator
Apache Felix
---
### Start
Download Apache Felix Framework Distribution
from <https://felix.apache.org/downloads.cgi>
org.apache.felix.main.distribution-5.6.8
No! The Apache Karaf container is best.
Download it from: <https://karaf.apache.org/download.html>
Download a binary distribution and unzip wherever you prefer.
Then run
bin\karaf.bat start
Unzip, pay attention to the files not being clipped(!).
system:exit
exit!
shutdown -h
or `^D`
### clean start
full clean, remove "data directory "
or...
bin\karaf.bat clean
bin\start.bat clean
### run mode
can be launched in
- the "regular" mode starts Apache Karaf in foreground, including the shell console.
- the "server" mode starts Apache Karaf in foreground, without the shell console.
- the "background" mode starts Apache Karaf in background.
### Logging
https://karaf.apache.org/manual/latest/#_log
can be logged to console
### Bundle deploy
bundle:install mvn:com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT
install mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT
install mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT
Eclipse's Equinox
====
Eclipse's OSGi platform
http://www.eclipse.org/equinox/
http://www.eclipse.org/equinox/documents/quickstart-framework.php
click on "download"
Latest Release
Oxygen.1 Wed, 6 Sep 2017 -- 17:00 (-0400)
org.eclipse.osgi_3.12.1.v20170821-1548.jar
= = NOT GOOD = =
+55
View File
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- com.baeldung/osgi-intro-sample-activator/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<artifactId>osgi-intro-sample-activator</artifactId>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- Qualified name of the class that exposes the activator iface. -->
<Bundle-Activator>com.baeldung.osgi.sample.activator.HelloWorld</Bundle-Activator>
<!-- One important thing to note:
since you are not exporting the package "com.baeldung.osgi.sample.activator",
you should at least add it to the Private-Package instruction.
Otherwise, the classes inside the package will not be copied to your bundle,
as the default value of this instruction is empty. -->
<Private-Package>com.baeldung.osgi.sample.activator</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,16 @@
package com.baeldung.osgi.sample.activator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class HelloWorld implements BundleActivator {
public void start(BundleContext ctx) {
System.out.println("Hello World.");
}
public void stop(BundleContext bundleContext) {
System.out.println("Goodbye World.");
}
}
+49
View File
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
<artifactId>osgi-intro-sample-client</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>osgi-intro-sample-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.client.Client</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.client</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,44 @@
package com.baeldung.osgi.sample.client;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.*;
public class Client implements BundleActivator, ServiceListener {
private BundleContext ctx;
private ServiceReference serviceReference;
public void start(BundleContext ctx) {
this.ctx = ctx;
try {
ctx.addServiceListener(this, "(objectclass=" + Greeter.class.getName() + ")");
} catch (InvalidSyntaxException ise) {
ise.printStackTrace();
}
}
public void stop(BundleContext bundleContext) {
if (serviceReference != null) {
ctx.ungetService(serviceReference);
}
this.ctx = null;
}
public void serviceChanged(ServiceEvent serviceEvent) {
int type = serviceEvent.getType();
switch (type) {
case (ServiceEvent.REGISTERED):
System.out.println("Notification of service registered.");
serviceReference = serviceEvent.getServiceReference();
Greeter service = (Greeter) (ctx.getService(serviceReference));
System.out.println(service.sayHiTo("John"));
break;
case (ServiceEvent.UNREGISTERING):
System.out.println("Notification of service unregistered.");
ctx.ungetService(serviceEvent.getServiceReference());
break;
default:
break;
}
}
}
+46
View File
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- mvn:com.baeldung/osgi-intro-sample-service/1.0-SNAPSHOT -->
<parent>
<artifactId>osgi-intro</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>osgi-intro-sample-service</artifactId>
<!-- Please, note this is not the usual 'jar'. -->
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.baeldung.osgi.sample.service.implementation.GreeterImpl</Bundle-Activator>
<Private-Package>com.baeldung.osgi.sample.service.implementation</Private-Package>
<Export-Package>com.baeldung.osgi.sample.service.definition</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,7 @@
package com.baeldung.osgi.sample.service.definition;
public interface Greeter {
public String sayHiTo(String name);
}
@@ -0,0 +1,30 @@
package com.baeldung.osgi.sample.service.implementation;
import com.baeldung.osgi.sample.service.definition.Greeter;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import java.util.Hashtable;
public class GreeterImpl implements Greeter, BundleActivator {
private ServiceReference<Greeter> reference;
private ServiceRegistration<Greeter> registration;
@Override public String sayHiTo(String name) {
return "Hello " + name;
}
@Override public void start(BundleContext context) throws Exception {
System.out.println("Registering service.");
registration = context.registerService(Greeter.class, new GreeterImpl(), new Hashtable<String, String>());
reference = registration.getReference();
}
@Override public void stop(BundleContext context) throws Exception {
System.out.println("Unregistering service.");
registration.unregister();
}
}
+87
View File
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>osgi-intro</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>osgi-intro-sample-activator</module>
<module>osgi-intro-sample-service</module>
<module>osgi-intro-sample-client</module>
</modules>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-gxyz</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>osgi-intro-mapquest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

Some files were not shown because too many files have changed in this diff Show More