StoreFactoryBase.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.catalina.storeconfig;

  18. import java.io.IOException;
  19. import java.io.PrintWriter;

  20. import org.apache.juli.logging.Log;
  21. import org.apache.juli.logging.LogFactory;
  22. import org.apache.tomcat.util.res.StringManager;

  23. /**
  24.  * StoreFactory saves special elements. Output was generate with StoreAppenders.
  25.  */
  26. public class StoreFactoryBase implements IStoreFactory {
  27.     private static Log log = LogFactory.getLog(StoreFactoryBase.class);

  28.     private StoreRegistry registry;

  29.     private StoreAppender storeAppender = new StoreAppender();

  30.     /**
  31.      * The string manager for this package.
  32.      */
  33.     protected static final StringManager sm = StringManager.getManager(Constants.Package);

  34.     /**
  35.      * The descriptive information string for this implementation.
  36.      */
  37.     private static final String info = "org.apache.catalina.config.StoreFactoryBase/1.0";

  38.     /**
  39.      * @return descriptive information about this Factory implementation and the corresponding version number, in the
  40.      *             format <code>&lt;description&gt;/&lt;version&gt;</code>.
  41.      */
  42.     public String getInfo() {
  43.         return info;
  44.     }

  45.     @Override
  46.     public StoreAppender getStoreAppender() {
  47.         return storeAppender;
  48.     }

  49.     @Override
  50.     public void setStoreAppender(StoreAppender storeAppender) {
  51.         this.storeAppender = storeAppender;
  52.     }

  53.     @Override
  54.     public void setRegistry(StoreRegistry aRegistry) {
  55.         registry = aRegistry;

  56.     }

  57.     @Override
  58.     public StoreRegistry getRegistry() {

  59.         return registry;
  60.     }

  61.     @Override
  62.     public void storeXMLHead(PrintWriter aWriter) {
  63.         // Store the beginning of this element
  64.         aWriter.print("<?xml version=\"1.0\" encoding=\"");
  65.         aWriter.print(getRegistry().getEncoding());
  66.         aWriter.println("\"?>");
  67.     }

  68.     @Override
  69.     public void store(PrintWriter aWriter, int indent, Object aElement) throws Exception {

  70.         StoreDescription elementDesc = getRegistry().findDescription(aElement.getClass());

  71.         if (elementDesc != null) {
  72.             if (log.isTraceEnabled()) {
  73.                 log.trace(sm.getString("factory.storeTag", elementDesc.getTag(), aElement));
  74.             }
  75.             getStoreAppender().printIndent(aWriter, indent + 2);
  76.             if (!elementDesc.isChildren()) {
  77.                 getStoreAppender().printTag(aWriter, indent, aElement, elementDesc);
  78.             } else {
  79.                 getStoreAppender().printOpenTag(aWriter, indent + 2, aElement, elementDesc);
  80.                 storeChildren(aWriter, indent + 2, aElement, elementDesc);
  81.                 getStoreAppender().printIndent(aWriter, indent + 2);
  82.                 getStoreAppender().printCloseTag(aWriter, elementDesc);
  83.             }
  84.         } else {
  85.             log.warn(sm.getString("factory.storeNoDescriptor", aElement.getClass()));
  86.         }
  87.     }

  88.     /**
  89.      * Must Implement at subclass for custom store children handling.
  90.      *
  91.      * @param aWriter     Current output writer
  92.      * @param indent      Indentation level
  93.      * @param aElement    Current element
  94.      * @param elementDesc The element description
  95.      *
  96.      * @throws Exception Configuration storing error
  97.      */
  98.     public void storeChildren(PrintWriter aWriter, int indent, Object aElement, StoreDescription elementDesc)
  99.             throws Exception {
  100.     }

  101.     /**
  102.      * Store only elements from storeChildren methods that are not a transient child.
  103.      *
  104.      * @param aWriter     Current output writer
  105.      * @param indent      Indentation level
  106.      * @param aTagElement Current element
  107.      *
  108.      * @throws Exception Configuration storing error
  109.      */
  110.     protected void storeElement(PrintWriter aWriter, int indent, Object aTagElement) throws Exception {
  111.         if (aTagElement != null) {
  112.             IStoreFactory elementFactory = getRegistry().findStoreFactory(aTagElement.getClass());

  113.             if (elementFactory != null) {
  114.                 StoreDescription desc = getRegistry().findDescription(aTagElement.getClass());
  115.                 if (desc != null) {
  116.                     if (!desc.isTransientChild(aTagElement.getClass().getName())) {
  117.                         elementFactory.store(aWriter, indent, aTagElement);
  118.                     }
  119.                 } else {
  120.                     log.warn(sm.getString("factory.storeNoDescriptor", aTagElement.getClass()));
  121.                 }
  122.             } else {
  123.                 log.warn(sm.getString("factory.storeNoDescriptor", aTagElement.getClass()));
  124.             }
  125.         }
  126.     }

  127.     /**
  128.      * Save an array of elements.
  129.      *
  130.      * @param aWriter  Current output writer
  131.      * @param indent   Indentation level
  132.      * @param elements Array of elements
  133.      *
  134.      * @throws Exception Configuration storing error
  135.      */
  136.     protected void storeElementArray(PrintWriter aWriter, int indent, Object[] elements) throws Exception {
  137.         if (elements != null) {
  138.             for (Object element : elements) {
  139.                 try {
  140.                     storeElement(aWriter, indent, element);
  141.                 } catch (IOException ioe) {
  142.                     // ignore children report error them self!
  143.                     // see StandardContext.storeWithBackup()
  144.                 }
  145.             }
  146.         }
  147.     }
  148. }