use of com.cosylab.CDB.DALChangeListener in project ACS by ACS-Community.
the class TestJDAL method testChangeListener.
/**
* Test ChangeListener
* Creates a changeListener object and registers for a curl
* then invokes clear_cache for that function in order to get
* a call from JDAL server
*/
public void testChangeListener() {
try {
// we will have change listener so we have to activate POA
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
// create our listener
ChangeListener changeListener = new ChangeListener();
DALChangeListener cl = changeListener._this(orb);
// register it
int listenerID = dal.add_change_listener(cl);
// tell add that we will listen for 'test'
dal.listen_for_changes("test", listenerID);
// our change listener will set objectChanged
objectChanged = false;
dal.clear_cache("test");
// give DAL chance (1s) to call us
for (int i = 0; i < 10; i++) {
Thread.sleep(100);
if (objectChanged)
break;
}
// by now the DAL should call us
assertTrue(objectChanged);
// remove it from DAL listeners
dal.remove_change_listener(listenerID);
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.cosylab.CDB.DALChangeListener in project ACS by ACS-Community.
the class HibernateWDALImpl method loadListeners.
/**
*
*/
public void loadListeners() {
File storageFile = getStorageFile();
if (storageFile == null || !storageFile.exists())
return;
try {
InputStream in = new FileInputStream(storageFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// load listeners
String line;
while (true) {
line = reader.readLine();
if (line == null || line.length() == 0)
break;
Integer id = new Integer(line);
line = reader.readLine();
if (line == null || line.length() == 0)
break;
// try to narrow it
DALChangeListener listener = DALChangeListenerHelper.narrow(orb.string_to_object(line));
synchronized (regListeners) {
regListeners.put(id, listener);
}
}
// then listened curls
String curl;
while (true) {
curl = reader.readLine();
if (curl == null)
break;
ArrayList<Integer> arr = new ArrayList<Integer>();
while (true) {
line = reader.readLine();
if (line == null || line.length() == 0)
break;
arr.add(new Integer(line));
}
synchronized (listenedCurls) {
listenedCurls.put(curl, arr);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.cosylab.CDB.DALChangeListener in project ACS by ACS-Community.
the class DALImpl method clear_cache.
public void clear_cache(String curl) {
totalDALInvocationCounter.incrementAndGet();
// first take care of our own map
object_changed(curl);
// then all registered listeners
ArrayList<Integer> listeners;
boolean needToSave = false;
synchronized (listenedCurls) {
listeners = listenedCurls.get(curl);
}
if (listeners == null)
return;
ArrayList<Integer> invalidListeners = new ArrayList<Integer>();
for (int i = 0; i < listeners.size(); i++) {
DALChangeListener listener;
synchronized (listenedCurls) {
listener = regListeners.get(listeners.get(i));
}
try {
//System.out.println("Calling " + listener + " ...");
listener.object_changed(curl);
//System.out.println("Done " + listener);
} catch (RuntimeException e) {
// silent here because who knows what happened with clients
invalidListeners.add(listeners.get(i));
}
}
synchronized (listenedCurls) {
// now remove invalid listeners if any
for (int i = 0; i < invalidListeners.size(); i++) {
listeners.remove(invalidListeners.get(i));
regListeners.remove(invalidListeners.get(i));
needToSave = true;
}
if (needToSave) {
cleanListenedCurls();
saveListeners();
}
}
}
use of com.cosylab.CDB.DALChangeListener in project ACS by ACS-Community.
the class DALImpl method cleanListenedCurls.
/**
* Cleans listened curls from invalid listeners
* to avoid repeatedly calling invalid listeners
*/
protected void cleanListenedCurls() {
Iterator<String> iter = listenedCurls.keySet().iterator();
while (iter.hasNext()) {
String curl = iter.next();
ArrayList<Integer> listeners = listenedCurls.get(curl);
if (listeners == null)
continue;
for (int i = 0; i < listeners.size(); i++) {
DALChangeListener listener = regListeners.get(listeners.get(i));
if (listener == null) {
listeners.remove(i);
i--;
}
}
}
}
use of com.cosylab.CDB.DALChangeListener in project ACS by ACS-Community.
the class DALImpl method loadListeners.
/**
*
*/
public void loadListeners() {
if (disableRecoveryFile)
return;
File storageFile = getStorageFile();
if (storageFile == null || !storageFile.exists()) {
return;
}
BufferedReader reader = null;
try {
InputStream in = new FileInputStream(storageFile);
reader = new BufferedReader(new InputStreamReader(in));
synchronized (listenedCurls) {
// load listeners
String line;
while (true) {
line = reader.readLine();
if (line == null || line.length() == 0)
break;
Integer id = new Integer(line);
line = reader.readLine();
if (line == null || line.length() == 0)
break;
// try to narrow it
DALChangeListener listener = DALChangeListenerHelper.narrow(orb.string_to_object(line));
regListeners.put(id, listener);
}
// then listened curls
String curl;
while (true) {
curl = reader.readLine();
if (curl == null)
break;
ArrayList<Integer> arr = new ArrayList<Integer>();
while (true) {
line = reader.readLine();
if (line == null || line.length() == 0)
break;
arr.add(new Integer(line));
}
listenedCurls.put(curl, arr);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Aggregations