Jako przykład zostanie przedstawiony prosty konektor realizujący odczytywanie linii wskazanego pliku. Wykorzystana zostanie klasa RandomAccessFile. Dodatkowe udostępnione operacje to zwrócenie bieżącej pozycji w pliku (getFilePointer) oraz ustawienie nowej pozycji (seek).
Implementacja konektora:
package palio.connectors;
import java.util.Properties;
import palio.PalioException;
public class SamplePooledConnector extends PooledConnector {
private final String filePath;
/**
* Connector constructor
*
* @param url URL in following format: file://tmp/lorem_ipsum.txt
* @param properties Connector properties
* @throws PalioException
*/
public SamplePooledConnector(String url, Properties properties) throws PalioException {
super(url, properties);
filePath = url.substring(("file://".length() - 1));
}
@Override
protected PooledConnection newConnection(PooledConnection conn) throws Exception {
if (conn == null) {
return new SamplePooledConnection(this);
} else {
return conn;
}
}
@Override
void refreshConnection(PooledConnection connection) throws Exception {
// Do nothing
}
public String getFilePath() {
return filePath;
}
public String readLine() throws PalioException {
SamplePooledConnection conn = null;
try {
conn = (SamplePooledConnection) getConnection();
return conn.getRaf().readLine();
} catch (Exception ex) {
throw new PalioException(ex);
} finally {
if (conn != null) {
putConnection(conn);
}
}
}
public void seek(Long position) throws PalioException {
SamplePooledConnection conn = null;
try {
conn = (SamplePooledConnection) getConnection();
conn.getRaf().seek(position);
} catch (Exception ex) {
throw new PalioException(ex);
} finally {
if (conn != null) {
putConnection(conn);
}
}
}
public Long getFilePointer() throws PalioException {
SamplePooledConnection conn = null;
try {
conn = (SamplePooledConnection) getConnection();
return conn.getRaf().getFilePointer();
} catch (Exception ex) {
throw new PalioException(ex);
} finally {
if (conn != null) {
putConnection(conn);
}
}
}
}
Implementacja klasy reprezentującej połączenie:
package palio.connectors;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import palio.PalioException;
public class SamplePooledConnection extends PooledConnection {
private RandomAccessFile raf;
SamplePooledConnection(PooledConnector connector) throws FileNotFoundException {
super(connector);
raf = new RandomAccessFile(((SamplePooledConnector) connector).getFilePath(), "r");
super.init();
}
public RandomAccessFile getRaf() {
return raf;
}
@Override
protected void closeParaller() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
closeNormal();
}
});
thread.setDaemon(true);
thread.start();
}
@Override
protected void closeNormal() {
try {
raf.close();
} catch (IOException ex) {
// Do some exception handling
ex.printStackTrace();
}
}
@Override
protected void setDedicated() throws PalioException {
dedicated = true;
}
@Override
protected void unsetDedicated() {
dedicated = false;
}
@Override
protected void resetExtend() {
if (raf != null) {
try {
raf.seek(0);
} catch (IOException ex) {
// Do some exception handling
ex.printStackTrace();
}
}
}
}
Moduł udostępniający API konektora:
package palio.modules;
import java.util.Properties;
import palio.Instance;
import palio.ModuleManager;
import palio.PalioException;
import palio.connectors.SamplePooledConnector;
import palio.modules.core.Module;
public class SamplePooledConnectorModule extends Module {
static {
ModuleManager.registerModule("sample", SamplePooledConnectorModule.class, ModuleManager.MODULE_STANDARD);
}
public SamplePooledConnectorModule(Instance instance, Properties parameters) {
super(instance, parameters);
}
@Override
public String getVersion() {
return "1.0.0";
}
public String nextLine(String connectorName) throws PalioException {
return ((SamplePooledConnector) instance.getConnector(connectorName)).readLine();
}
public void seek(String connectorName, Long position) throws PalioException {
((SamplePooledConnector) instance.getConnector(connectorName)).seek(position);
}
public Long getFilePointer(String connectorName) throws PalioException {
return ((SamplePooledConnector) instance.getConnector(connectorName)).getFilePointer();
}
}
Przykład użycia modułu:
$sample.seek("sample", 120) $sample.nextLine("sample") $sample.getFilePointer("sample")