Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions jca/connector-simple/connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,4 @@
<version>1.0-SNAPSHOT</version>
<name>connector</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions jca/mdb-filewatcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## File Watcher MDB

### What is this?

This sample project demonstrates that writing (and testing) JCA resource adapter is fairly easy. We implemented Message Driven Bean which observes certain directory for files which are created, updated or deleted.

It's tested using:

* [Arquillian](http://arquillian.org) - powerful testing middleware
* [Awaitility](https://code.google.com/p/awaitility/) - simple, yet powerful DSL that allows you to express expectations of an asynchronous system in a concise and easy to read manner


This sample project is based on Robert Panzer [work](https://github.com/robertpanzer/filesystemwatch-connector) ([read the full blog post by Robert here](http://robertpanzer.github.io/blog/2014/inboundra-nointfmdbs.html)).
14 changes: 14 additions & 0 deletions jca/mdb-filewatcher/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>
<parent>
<artifactId>jca-samples</artifactId>
<groupId>org.javaee7.jca</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.javaee7</groupId>
<artifactId>mdb-filewatcher</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License 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 org.javaee7.jca.filewatch.adapter;

/**
* @author Robert Panzer (robert.panzer@me.com)
*/
public interface FileSystemWatcher {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License 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 org.javaee7.jca.filewatch.adapter;

import javax.resource.ResourceException;
import javax.resource.spi.Activation;
import javax.resource.spi.ActivationSpec;
import javax.resource.spi.InvalidPropertyException;
import javax.resource.spi.ResourceAdapter;

/**
* @author Robert Panzer (robert.panzer@me.com)
*/
@Activation(messageListeners = FileSystemWatcher.class)
public class FileSystemWatcherActivationSpec implements ActivationSpec {

private ResourceAdapter resourceAdapter;

private String dir;

@Override
public ResourceAdapter getResourceAdapter() {
return resourceAdapter;
}

@Override
public void setResourceAdapter(ResourceAdapter resourceAdapter)
throws ResourceException {
this.resourceAdapter = resourceAdapter;
}

@Override
public void validate() throws InvalidPropertyException {

}

public String getDir() {
return dir;
}

public void setDir(String dir) {
this.dir = dir;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License 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 org.javaee7.jca.filewatch.adapter;

import javax.resource.ResourceException;
import javax.resource.spi.*;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.transaction.xa.XAResource;
import java.io.IOException;
import java.nio.file.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Robert Panzer (robert.panzer@me.com)
* @author Bartosz Majsak (bartosz.majsak@gmail.com)
*/
@Connector
public class FileSystemWatcherResourceAdapter implements ResourceAdapter {

FileSystem fileSystem;

WatchService watchService;

Map<WatchKey, MessageEndpointFactory> listeners = new ConcurrentHashMap<>();

Map<MessageEndpointFactory, Class<?>> endpointFactoryToBeanClass = new ConcurrentHashMap<>();

private BootstrapContext bootstrapContext;

@Override
public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec) throws ResourceException {
FileSystemWatcherActivationSpec fsWatcherAS = (FileSystemWatcherActivationSpec) activationSpec;

try {
WatchKey watchKey = fileSystem.getPath(fsWatcherAS.getDir())
.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY);

listeners.put(watchKey, endpointFactory);

endpointFactoryToBeanClass.put(endpointFactory, endpointFactory.getEndpointClass());
} catch (IOException e) {
throw new ResourceException(e);
}
}

@Override
public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec) {
for (WatchKey watchKey: listeners.keySet()) {
if (listeners.get(watchKey) == endpointFactory) {
listeners.remove(watchKey);
break;
}
}
endpointFactoryToBeanClass.remove(endpointFactory);
}

@Override
public XAResource[] getXAResources(ActivationSpec[] arg0) throws ResourceException {
return new XAResource[0];
}

@Override
public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException {
this.bootstrapContext = bootstrapContext;

try {
fileSystem = FileSystems.getDefault();
watchService = fileSystem.newWatchService();
} catch (IOException e) {
throw new ResourceAdapterInternalException(e);
}

new WatchingThread(watchService, this).start();
}

@Override
public void stop() {
try {
watchService.close();
} catch (IOException e) {
throw new RuntimeException("Failed stopping file watcher.", e);
}
}

public MessageEndpointFactory getListener(WatchKey watchKey) {
return listeners.get(watchKey);
}

public BootstrapContext getBootstrapContext() {
return bootstrapContext;
}

public Class<?> getBeanClass(MessageEndpointFactory endpointFactory) {
return endpointFactoryToBeanClass.get(endpointFactory);
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License 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 org.javaee7.jca.filewatch.adapter;

import javax.resource.spi.endpoint.MessageEndpoint;
import javax.resource.spi.endpoint.MessageEndpointFactory;
import javax.resource.spi.work.Work;
import javax.resource.spi.work.WorkException;
import java.lang.reflect.Method;
import java.nio.file.*;
import java.util.List;

import org.javaee7.jca.filewatch.event.*;

/**
* @author Robert Panzer (robert.panzer@me.com)
* @author Bartosz Majsak (bartosz.majsak@gmail.com)
*/
final class WatchingThread extends Thread {

private WatchService watchService;

private FileSystemWatcherResourceAdapter resourceAdapter;

WatchingThread(WatchService watchService,
FileSystemWatcherResourceAdapter ra) {
this.watchService = watchService;
this.resourceAdapter = ra;
}

public void run() {
while (true) {
try {
WatchKey watchKey = watchService.take();
if (watchKey != null) {
dispatchEvents(watchKey.pollEvents(), resourceAdapter.getListener(watchKey));
watchKey.reset();
}
} catch (ClosedWatchServiceException e) {
return;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private void dispatchEvents(List<WatchEvent<?>> events, MessageEndpointFactory messageEndpointFactory) {
for (WatchEvent<?> event: events) {
Path path = (Path) event.context();

try {
MessageEndpoint endpoint = messageEndpointFactory.createEndpoint(null);
Class<?> beanClass = resourceAdapter.getBeanClass(messageEndpointFactory);
for (Method m: beanClass.getMethods()) {
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())
&& m.isAnnotationPresent(Created.class)
&& path.toString().matches(m.getAnnotation(Created.class).value())) {
invoke(endpoint, m, path);
} else if (StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())
&& m.isAnnotationPresent(Deleted.class)
&& path.toString().matches(m.getAnnotation(Deleted.class).value())) {
invoke(endpoint, m, path);
} else if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind())
&& m.isAnnotationPresent(Modified.class)
&& path.toString().matches(m.getAnnotation(Modified.class).value())) {
invoke(endpoint, m, path);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void invoke(final MessageEndpoint endpoint, final Method m, final Path path) throws WorkException {
resourceAdapter.getBootstrapContext().getWorkManager().scheduleWork(new Work() {

@Override
public void run() {
try {
Method endpointMethod = endpoint.getClass().getMethod(m.getName(), m.getParameterTypes());
endpoint.beforeDelivery(endpointMethod);

endpointMethod.invoke(endpoint, path.toFile());

endpoint.afterDelivery();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void release() {}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License 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 org.javaee7.jca.filewatch.event;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* @author Robert Panzer (robert.panzer@me.com)
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Created {

public String value() default ".*";

}
Loading