cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Cayenne Documentation > Web Applications
Date Tue, 22 Dec 2009 11:39:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAYDOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAYDOC/Web+Applications">Web
Applications</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~andrus">Andrus
Adamchik</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h3><a name="WebApplications-PuttingCayenneFilesinWebApplicationCLASSPATH"></a>Putting
Cayenne Files in Web Application CLASSPATH</h3>

<p>When deploying an application in a web container it is possible to follow the procedure
for the standalone applications, i.e. put all XML files in the application CLASSPATH (e.g.
in "mywebapp/WEB-INF/classes/", but DON'T put it in container shared locations!). Session
DataContext can be obtained via ServletUtil:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">HttpSession session = ...;
DataContext context = ServletUtil.getSessionContext(session);</pre>
</div></div>

<p>However you may consider a deployment procedure described below, as it provides more
flexibility and a number of additional useful features.</p>

<h3><a name="WebApplications-CayenneServletFilter"></a>Cayenne Servlet Filter</h3>

<p>Adding the following filter to the <tt>web.xml</tt> would automate Cayenne
setup in the web application:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>&lt;filter&gt;
    &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.apache.cayenne.conf.WebApplicationContextFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
</div></div>

<p>The filter will ensure that for each matching request a ObjectContext (which is actually
a DataContext instance) is bound to the request thread. Context itself is session-scoped.
So you can retrieve it anywhere in the application, even if you don't have a reference to
the web environment:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">ObjectContext context = BaseContext.getThreadObjectContext();</pre>
</div></div>

<p>Filter can match a specific URL as shown above, or a specific servlet:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>&lt;servlet&gt;
   &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
   &lt;servlet-class&gt;org.example.MyServlet&lt;/servlet-class&gt;
   &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
&lt;/servlet&gt;
		
&lt;filter&gt;
    &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.apache.cayenne.conf.WebApplicationContextFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;CayenneFilter&lt;/filter-name&gt;
    &lt;servlet-name&gt;MyServlet&lt;/servlet-name&gt;
&lt;/filter-mapping&gt;</pre>
</div></div>

<p>Additionally the filter supports putting files in <tt>myapp/WEB-INF/</tt>
directory instead of the CLASSPATH, so a .war file may look like this:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>index.jsp
WEB-INF/cayenne.xml
WEB-INF/xyz.map.xml
WEB-INF/lib/...</pre>
</div></div>

<p>Actually, Cayenne files can be stored in any subdirectory of <tt>myapp/WEB-INF/</tt>.
To specify a subdirectory, you'll need to add <tt>&lt;context-param&gt;</tt>
named <tt>cayenne.configuration.path</tt> to your application descriptor:</p>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>&lt;context-param&gt;
    &lt;param-name&gt;cayenne.configuration.path&lt;/param-name&gt;
    &lt;param-value&gt;/WEB-INF/config/cayenne-files&lt;/param-value&gt;
&lt;/context-param&gt;</pre>
</div></div>

<h3><a name="WebApplications-ObjectContextScope"></a>ObjectContext Scope</h3>

<p>ServletUtil and WebApplicationContextFilter would both provide an ObjectContext shared
between the requests in the same user session (session-scoped context). This is the default
behavior. However, depending on the application needs, you may choose to use application-scoped
context (usually for read-only parts of the application, and combined with caching), or per-request
context (to avoid race conditions on long-running operations). In those cases you won't be
able to use ServletUtil and WebApplicationContextFilter, but you can easily write your own
correctly scoped replacements, or set it up via an IoC container, such as Spring.</p>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Web+Applications">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=10534&revisedVersion=6&originalVersion=5">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAYDOC/Web+Applications?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message