blob: cb462e71654d09bcb8cee0bebbe085f436469520 [file] [log] [blame]
Title: Standalone Applications
<H3><A name="StandaloneApplications-SharedConfigurationSingleton"></A>Shared Configuration Singleton</H3>
<P>In a standalone Java application, DefaultConfiguration is used to locate and load configuration files. This approach does not require any additional setup. Shared configuration instance can simply be obtained by calling <TT>getSharedConfiguration()</TT>:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.conf.Configuration;
...
Configuration conf = Configuration.getSharedConfiguration();
</PRE>
</DIV></DIV>
<P>DefaultConfiguration will expect cayenne.xml file to be located in the CLASSPATH. The same is true for DataMaps referenced in cayenne.xml. Their location is resolved relative to CLASSPATH as well. To make these files available to Cayenne, you can simply include them in the root of your application jar file (read <A href="customizing-configuration.html" title="Customizing Configuration">Customizing Configuration</A> on how to change that).</P>
<P>The location of the data source files referenced in cayenne.xml is interpreted by the factory that was assigned to each datasource in cayenne.xml. Default factory is DriverDataSourceFactory. It will rely on its parent configuration to find the data source file (i.e. locating data source files will be no different from the DataMap files). Depending on the application needs, programmers may implement custom factories that collect database information interactively by showing a login dialog, etc.</P>
<H3><A name="StandaloneApplications-SharedConfigurationandDataContext"></A>Shared Configuration and DataContext</H3>
<P>In a setup that relies on shared Configuration a DataContext can be created by calling <TT>DataContext.createDataContext()</TT> static method that internally uses shared configuration:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.access.DataContext;
...
DataContext context = DataContext.createDataContext();
</PRE>
</DIV></DIV>
<P>Later a DataContext can be passed around in the code explicitly, or it can be bound to an execution thread, making it accessible to all code being run within this thread (e.g. this can be a Swing event thread):</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.access.DataContext;
...
DataContext context = DataContext.createDataContext();
DataContext.bindThreadDataContext(context);
</PRE>
</DIV></DIV>