Search in sources :

Example 1 with DALChangeListener

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();
    }
}
Also used : POA(org.omg.PortableServer.POA) DALChangeListenerPOA(com.cosylab.CDB.DALChangeListenerPOA) DALChangeListener(com.cosylab.CDB.DALChangeListener) DALChangeListener(com.cosylab.CDB.DALChangeListener) NamingException(javax.naming.NamingException)

Example 2 with DALChangeListener

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) DALChangeListener(com.cosylab.CDB.DALChangeListener) File(java.io.File) FileInputStream(java.io.FileInputStream) LSException(org.w3c.dom.ls.LSException) HibernateException(org.hibernate.HibernateException) DOMException(org.w3c.dom.DOMException) LogConfigException(alma.acs.logging.config.LogConfigException)

Example 3 with DALChangeListener

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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DALChangeListener(com.cosylab.CDB.DALChangeListener)

Example 4 with DALChangeListener

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--;
            }
        }
    }
}
Also used : DALChangeListener(com.cosylab.CDB.DALChangeListener)

Example 5 with DALChangeListener

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();
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) DALChangeListener(com.cosylab.CDB.DALChangeListener) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) LogConfigException(alma.acs.logging.config.LogConfigException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException)

Aggregations

DALChangeListener (com.cosylab.CDB.DALChangeListener)8 ArrayList (java.util.ArrayList)5 LogConfigException (alma.acs.logging.config.LogConfigException)2 DALChangeListenerPOA (com.cosylab.CDB.DALChangeListenerPOA)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 CBDescIn (alma.ACS.CBDescIn)1 CBvoid (alma.ACS.CBvoid)1 OffShoot (alma.ACS.OffShoot)1 RWdouble (alma.ACS.RWdouble)1 Lamp (alma.acsexmplLamp.Lamp)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 NamingException (javax.naming.NamingException)1 HibernateException (org.hibernate.HibernateException)1 POA (org.omg.PortableServer.POA)1 DOMException (org.w3c.dom.DOMException)1