001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.view;
018    
019    import javax.jbi.servicedesc.ServiceEndpoint;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.apache.servicemix.jbi.event.EndpointEvent;
024    import org.apache.servicemix.jbi.event.EndpointListener;
025    
026    /**
027     * A base class for renderings of endpoints which can re-render whenever something
028     * changes or mark things as dirty so that they can be re-rendered on demand or in a time based way
029     * 
030     * @version $Revision: 564607 $
031     */
032    public abstract class EndpointViewRenderer implements EndpointListener {
033    
034        private static final Log LOG = LogFactory.getLog(EndpointViewRenderer.class);
035        
036        private boolean dirty;
037        private boolean rerenderOnChange = true;
038    
039        public void render() throws Exception {
040            doRender();
041            dirty = false;
042        }
043    
044        public void internalEndpointRegistered(EndpointEvent event) {
045            viewIsDirty(event.getEndpoint());
046        }
047    
048        public void internalEndpointUnregistered(EndpointEvent event) {
049            viewIsDirty(event.getEndpoint());
050        }
051    
052        public void externalEndpointRegistered(EndpointEvent event) {
053            viewIsDirty(event.getEndpoint());
054        }
055    
056        public void externalEndpointUnregistered(EndpointEvent event) {
057            viewIsDirty(event.getEndpoint());
058        }
059    
060        public void linkedEndpointRegistered(EndpointEvent event) {
061            viewIsDirty(event.getEndpoint());
062        }
063    
064        public void linkedEndpointUnregistered(EndpointEvent event) {
065            viewIsDirty(event.getEndpoint());
066        }
067    
068        public void remoteEndpointRegistered(EndpointEvent event) {
069            viewIsDirty(event.getEndpoint());
070        }
071    
072        public void remoteEndpointUnregistered(EndpointEvent event) {
073            viewIsDirty(event.getEndpoint());
074        }
075    
076        // Properties
077        // -------------------------------------------------------------------------
078        public boolean isRerenderOnChange() {
079            return rerenderOnChange;
080        }
081    
082        public void setRerenderOnChange(boolean rerenderOnChange) {
083            this.rerenderOnChange = rerenderOnChange;
084        }
085    
086        public boolean isDirty() {
087            return dirty;
088        }
089    
090        // Implementation methods
091        // -------------------------------------------------------------------------
092        protected abstract void doRender() throws Exception;
093    
094        protected void viewIsDirty(ServiceEndpoint endpoint) {
095            dirty = true;
096            if (rerenderOnChange) {
097                try {
098                    render();
099                } catch (Exception e) {
100                    LOG.warn("Failed to render view: " + e, e);
101                }
102            }
103        }
104    
105    }