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.camel.component.xslt;
018    
019    import java.io.IOException;
020    import javax.xml.transform.Source;
021    import javax.xml.transform.TransformerException;
022    
023    import org.apache.camel.Component;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.api.management.ManagedAttribute;
026    import org.apache.camel.api.management.ManagedOperation;
027    import org.apache.camel.api.management.ManagedResource;
028    import org.apache.camel.builder.xml.XsltBuilder;
029    import org.apache.camel.component.timer.TimerConsumer;
030    import org.apache.camel.impl.ProcessorEndpoint;
031    import org.apache.camel.spi.UriEndpoint;
032    import org.apache.camel.spi.UriParam;
033    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    @ManagedResource(description = "Managed XsltEndpoint")
037    @UriEndpoint(scheme = "xslt")
038    public class XsltEndpoint extends ProcessorEndpoint {
039    
040        private static final Logger LOG = LoggerFactory.getLogger(XsltEndpoint.class);
041    
042        private volatile boolean cacheCleared;
043        private XsltBuilder xslt;
044        @UriParam
045        private String resourceUri;
046        @UriParam
047        private boolean cacheStylesheet;
048    
049        public XsltEndpoint(String endpointUri, Component component, XsltBuilder xslt, String resourceUri,
050                boolean cacheStylesheet) throws Exception {
051            super(endpointUri, component, xslt);
052            this.xslt = xslt;
053            this.resourceUri = resourceUri;
054            this.cacheStylesheet = cacheStylesheet;
055        }
056    
057        @ManagedOperation(description = "Clears the cached XSLT stylesheet, forcing to re-load the stylesheet on next request")
058        public void clearCachedStylesheet() {
059            this.cacheCleared = true;
060        }
061    
062        @ManagedAttribute(description = "Whether the XSLT stylesheet is cached")
063        public boolean isCacheStylesheet() {
064            return cacheStylesheet;
065        }
066    
067        @ManagedAttribute(description = "Endpoint State")
068        public String getState() {
069            return getStatus().name();
070        }
071    
072        @ManagedAttribute(description = "Camel ID")
073        public String getCamelId() {
074            return getCamelContext().getName();
075        }
076    
077        @ManagedAttribute(description = "Camel ManagementName")
078        public String getCamelManagementName() {
079            return getCamelContext().getManagementName();
080        }
081    
082        public XsltEndpoint findOrCreateEndpoint(String uri, String newResourceUri) {
083            String newUri = uri.replace(resourceUri, newResourceUri);
084            LOG.trace("Getting endpoint with URI: {}", newUri);
085            return getCamelContext().getEndpoint(newUri, XsltEndpoint.class);
086        }
087    
088        @Override
089        protected void onExchange(Exchange exchange) throws Exception {
090    
091            if (!cacheStylesheet || cacheCleared) {
092                loadResource(resourceUri);
093            }
094            super.onExchange(exchange);
095    
096        }
097    
098        /**
099         * Loads the resource.
100         *
101         * @param resourceUri  the resource to load
102         *
103         * @throws TransformerException is thrown if error loading resource
104         * @throws IOException is thrown if error loading resource
105         */
106        protected void loadResource(String resourceUri) throws TransformerException, IOException {
107            LOG.trace("{} loading schema resource: {}", this, resourceUri);
108            Source source = xslt.getUriResolver().resolve(resourceUri, null);
109            if (source == null) {
110                throw new IOException("Cannot load schema resource " + resourceUri);
111            } else {
112                xslt.setTransformerSource(source);
113            }
114            // now loaded so clear flag
115            cacheCleared = false;
116        }
117    
118        @Override
119        protected void doStart() throws Exception {
120            super.doStart();
121            loadResource(resourceUri);
122        }
123    
124        @Override
125        protected void doStop() throws Exception {
126            super.doStop();
127        }
128    }