Re: Connecting to deployment database for development/testing
Re: Connecting to deployment database for development/testing
- Subject: Re: Connecting to deployment database for development/testing
- From: Zak Burke <email@hidden>
- Date: Thu, 30 Mar 2006 10:02:20 -0500
Tanmoy Roy wrote on 3/29/06 7:40 PM:
I am really fascinated by the way you have handled the connections in
EOModel. I have couple of questions regarding this :
1) Going by your code it seems that we can change the connection
dictionary at run-time. Am I correct ?
Yes.
2) If #1 is correct and as far as my little knowledge of WO and
EOModel goes we need to start and stop the app. instances in the
monitor for any change in the EOModel to be visible. Can you please
tell me am I correct here?
It is possible to update an app's model without restarting it in
WOMonitor, but that's certainly the easiest way. I would speculate that
if you're changing your model then you're probably changing your EO
classes, and you would need to restart to get those updates. Well, I
guess you could reload 'em via the class loader, but I digress.
Now a small request. Can you give me a code piece where you have
implemented the concept that you have mentioned.
Here are all the relevant parts of my Application.java class that allow
you to modify a model's connection dictionary on-the-fly when a model is
loaded. (WO automatically finds and loads any model in the deployment
directory.)
This is what works for me, but for all I know the code violates all
kinds of WO best practices. Hopefully, one of the gurus will step in and
correct the things that are egregiously wrong.
Good luck,
zak.
Application.java
=========================================
public Application()
{
super();
// copy run-time properties into System.properties
propertiesInit();
// log4j init, etc. ...
// connect to DB with runtime props instead of model's props
dbInit();
}
/**
* Copy runtime properties into System.properties.
*/
public void propertiesInit()
{
// I3pkbProps.PROPS_PATH is a constant containing the name of the
// the property containing the path to the properties file. That is,
// I3pkbProps.PROPS_PATH is something like "props.path" and the
// local Properties file contains a line like this:
// props.path = /path/to/application.properties
// My code only refers to properties by their constants so that the
// compiler can find my tyops.
I3pkbProps.init(System.getProperty(I3pkbProps.PROPS_PATH));
}
/**
* register to receieve model-added notificatons so we can intercept 'em
* and update the connection properties.
*/
private void dbInit()
{
Class params[] = { com.webobjects.foundation.NSNotification.class };
// set up a selector naming the method to call ("receiveModel...")
// and then set up this class as an observer of model-added
// notifications. that means this class must implement the
// receiveModel... method
NSSelector selector = new NSSelector(
"receiveModelAddedNotification", params
);
NSNotificationCenter.defaultCenter().addObserver(
this, selector, EOModelGroup.ModelAddedNotification, null
);
}
/**
* Intercept add-model notifications and reset the authentication
* fields in the model's connection dictionary if System.properties
* contains a username and password for this model.
*
* @param notification container for EOModel being added
*/
public void receiveModelAddedNotification(NSNotification notification)
{
EOModel model = (EOModel) notification.object();
String username = System.getProperty(model.name() + ".username");
String password = System.getProperty(model.name() + ".password");
if (username != null && password != null)
{
NSMutableDictionary connD =
model.connectionDictionary().mutableClone();
connD.setObjectForKey( username, "username");
connD.setObjectForKey( password, "password");
model.setConnectionDictionary(connD);
}
}
I3pkbProps.java
=========================================
/** pointer to external properties file */
public static final String PROPS_PATH = "props.path";
/**
* Load properties from the given file into System's properties
*
* @param filename name of a java.util.Properties-formatted file
*/
public static void init(String filename)
{
BufferedInputStream bis = null;
Properties temp = new Properties();
try
{
// load the properties file ...
bis = new BufferedInputStream(new FileInputStream(filename));
if (bis != null)
temp.load(bis);
// now copy key-value pairs into System properties
Enumeration keys = temp.propertyNames();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
String value = (String) temp.getProperty(key);
System.setProperty(key, value);
}
}
catch (FileNotFoundException e)
{
// log this
}
catch (IOException e)
{
// log this
}
}
On 3/28/06, Zak Burke <email@hidden> wrote:
John Huss wrote on 3/28/06 10:23 AM:
I was curious how most of you specify the URL of your database in
EOModeler. Do you use localhost, etc, or the actual address of the
server? What I mean is, do you connect to the production database
while developing and/or testing, or do you just use a local version?
I connect to localhost and use an external properties file to replace
the DB connection properties (username, password). For example, an app's
internal Properties file will contain only a single line:
props.path = /var/app_dir/app.properties
and the actual properties are all in app.properties. That file path,
/var/app_dir/app.properties, exists on both my dev and production
machines. By keeping an application's runtime props in a file that isn't
affected by each deployment, I don't have to worry about deploying an
app and forgetting to change its settings from dev to production.
Specifically WRT model parameters, my app.properties file contains the
following:
modelname.username = dbuser
modelname.password = dbpassword
modelname.database = dbname
and I have the following method in Application.java. I suppose I could
add a modelname.URL param as well to point to an external DB, but I
haven't tried that.
/**
* Intercept add-model notifications and reset the authentication
* fields in the model's connection dictionary if System.properties
* contains a username and password for this model.
*
* @param notification container for EOModel being added
*/
public void receiveModelAddedNotification(NSNotification notification)
{
EOModel model = (EOModel) notification.object();
String username = System.getProperty(model.name() + ".username");
String password = System.getProperty(model.name() + ".password");
if (username != null && password != null)
{
NSMutableDictionary connD =
model.connectionDictionary().mutableClone();
connD.setObjectForKey( username, "username");
connD.setObjectForKey( password, "password");
model.setConnectionDictionary(connD);
}
}
HTH,
zak.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden