View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.ojb;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.TagSupport;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.ojb.broker.PersistenceBroker;
22  import org.apache.ojb.broker.PersistenceBrokerFactory;
23  
24  /***
25   * <p>Tag handler for &lt;Driver&gt; in JSTL, used to create
26   * a simple DataSource for prototyping.</p>
27   *
28    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29    * @version $Revision: 155420 $
30   */
31  public class BrokerTag extends TagSupport {
32  
33      /*** The variable name to export. */
34      private String var;
35  
36      /*** The persistence broker instance */
37      private PersistenceBroker broker;
38  
39      public BrokerTag() {
40      }
41  
42  
43      // Tag interface
44      //-------------------------------------------------------------------------
45      public void doTag(XMLOutput output) throws JellyTagException {
46          if ( var == null ) {
47              var = "org.apache.commons.jelly.ojb.Broker";
48          }
49          if ( broker != null ) {
50              context.setVariable(var, broker);
51              invokeBody(output);
52          }
53          else {
54              broker = PersistenceBrokerFactory.defaultPersistenceBroker();
55              context.setVariable(var, broker);
56  
57              try {
58                  invokeBody(output);
59              }
60              finally {
61                  broker.close();
62                  broker = null;
63                  context.removeVariable(var);
64              }
65          }
66      }
67  
68      // Properties
69      //-------------------------------------------------------------------------
70      /*** Sets the variable name to define for this expression
71       */
72      public void setVar(String var) {
73          this.var = var;
74      }
75  
76      /*** @return the persistence broker instance */
77      public PersistenceBroker getBroker() {
78          return broker;
79      }
80  
81      /*** Sets the persistence broker instance */
82      public void setBroker(PersistenceBroker broker) {
83          this.broker = broker;
84      }
85  }