View Javadoc

1   /*
2    * $Id: AbstractContainerContext.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.impl.container;
12  
13  import org.mule.MuleManager;
14  import org.mule.umo.lifecycle.InitialisationException;
15  import org.mule.umo.manager.ContainerException;
16  import org.mule.umo.manager.UMOContainerContext;
17  import org.mule.util.ChainedReader;
18  import org.mule.util.SystemUtils;
19  
20  import java.io.Reader;
21  import java.io.StringReader;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * <code>AbstractContainerContext</code> provides base container configuration
28   * functions for handling embedded configuration.
29   */
30  public abstract class AbstractContainerContext implements UMOContainerContext
31  {
32      /**
33       * logger used by this class
34       */
35      protected transient Log logger = LogFactory.getLog(getClass());
36  
37      private String name;
38  
39      protected AbstractContainerContext(String name)
40      {
41          this.name = name;
42      }
43  
44      public String getName()
45      {
46          return name;
47      }
48  
49      public void setName(String name)
50      {
51          this.name = name;
52      }
53  
54      public void initialise() throws InitialisationException
55      {
56          // noop
57      }
58  
59      public void dispose()
60      {
61          // noop
62      }
63  
64      public final void configure(Reader configuration, String doctype, String encoding)
65          throws ContainerException
66      {
67          String decl = getXmlDeclaration(encoding);
68          logger.debug("Using Xml declaration: " + decl);
69          if (doctype == null)
70          {
71              doctype = getDefaultDocType();
72          }
73          if (doctype != null)
74          {
75              if (!doctype.startsWith("<!DOCTYPE"))
76              {
77                  doctype = "<!DOCTYPE " + doctype + ">";
78              }
79              logger.info("Using doctype: " + doctype);
80          }
81          else
82          {
83              doctype = "";
84          }
85          StringReader declaration = new StringReader(decl + SystemUtils.LINE_SEPARATOR + doctype);
86          ChainedReader reader = new ChainedReader(declaration, configuration);
87          configure(reader);
88  
89      }
90  
91      protected String getXmlDeclaration(String encoding)
92      {
93          if (encoding == null)
94          {
95              encoding = getDefaultEncoding();
96          }
97          return "<?xml version=\"1.0\" encoding=\"" + encoding.toUpperCase() + "\"?>";
98      }
99  
100     protected String getDefaultDocType()
101     {
102         return null;
103     }
104 
105     protected String getDefaultEncoding()
106     {
107         return MuleManager.getConfiguration().getEncoding();
108     }
109 
110     public abstract void configure(Reader configuration) throws ContainerException;
111 }