Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Any useful examples

An app that I'm working on uses properties defined on the command line.  For
usability, the application defaults some of them to a best-guess value so
that a user doesn't have to know the ins and outs of the app just to get it
running.  Unfortunately, due to timing issues (e.g., the app works with RMI
which doesn't set some properties until after the RMISecurityManager is
set), these defaults can't be easily set in a single place.  The calls to
the defaulting mechanism got scattered throughout the code.  At this point,
if I add another property that needs a default, where do I put the call?
What if it depends on the value of another property?  Simply, I have to play
with it until I find a place that works!  So, this suggests that setting
properties to default values is a cross-cutting concern.  I pull the
defaulting method into an aspect method, put the calls to the method into
advice, and define the pointcut to match on certain calls to
System.getProperty().  All of my properties that get defaulted are now in
ONE place!!  Want to add another default?  Just put it in the advice with
the others.  There is no simple way to do this using OO.

> -----Original Message-----
> From: aspectj-users-admin@xxxxxxxxxxx
> [mailto:aspectj-users-admin@xxxxxxxxxxx]On Behalf Of Kenworthy, Edward
> (EDS)
> Sent: Friday, August 08, 2003 2:55 AM
> To: 'aspectj-users@xxxxxxxxxxx'
> Subject: [aspectj-users] Any useful examples
>
>
> Been following Aspects for a year/18 months but never really dived in,
> principally because I still don't see the point ;-)
...


Here's the code:

------------------------------------
pointcut getProperty(String pProperty):
	call(static String System.getProperty(String)) &&
	args(pProperty) &&
	within(org.jini.judy.*) &&
	!withincode(boolean
org.jini.judy.JudyFunctionalAspects.propertyExists(String)) &&
	!withincode(void org.jini.judy.JudyFunctionalAspects.defaultTo*(String,
String));

before(String pProperty): getProperty(pProperty)
{
	if("webservice.storage.home".equals(pProperty))
	{
		defaultToValue("webservice.storage.home",
				System.getProperty("judy.home") +
				System.getProperty("file.separator") +
				"webservices");
	}
	else if("judy.scripts.home".equals(pProperty))
	{
		defaultToValue("judy.scripts.home",
				System.getProperty("judy.home") +
				System.getProperty("file.separator") +
				"scripts");
	}
	else if("ant.home".equals(pProperty))
	{
		defaultToValue("ant.home",
				System.getProperty("judy.home") +
				System.getProperty("file.separator") +
				"lib");
	}
	else if("java.security.policy".equals(pProperty))
	{
		defaultToValue("java.security.policy",
				System.getProperty("judy.home") +
				System.getProperty("file.separator") +
				"conf" +
				System.getProperty("file.separator") +
				"all.policy");
	}
	else if("judy.axis.codebase.server".equals(pProperty))
	{
		defaultToProperty("judy.axis.codebase.server",
"java.rmi.server.codebase");
	}
	else if("judy.home".equals(pProperty))
	{
		defaultToProperty("judy.home", "user.dir");
	}
}

private boolean propertyExists(String pProperty)
{
	String lProperty = System.getProperty(pProperty);
	if("".equals(lProperty) || lProperty == null)
	{
		return false;
	}
	return true;
}

private void defaultToProperty(String pProperty, String pDefaultProperty)
{
	if(!propertyExists(pProperty))
	{
		System.out.println(pProperty + " is not defined.\n\t" +
			"Defaulting to " + pDefaultProperty + " [" +
			System.getProperty(pDefaultProperty) + "]");
		System.setProperty(pProperty, System.getProperty(pDefaultProperty));
	}
}

private void defaultToValue(String pProperty, String pDefaultValue)
{
	if(!propertyExists(pProperty))
	{
		System.out.println(pProperty + " is not defined.\n\t" +
			"Defaulting to [" +
			pDefaultValue + "]");
		System.setProperty(pProperty, pDefaultValue);
	}
}
------------------------------------

An interesting (but non-obvious) point to note is that the pointcut matches
on System.getProperty() calls in the before() advice.  This makes the order
of defaulting unimportant!  Another nice side-effect is that the console is
spitting out that a property is being defaulted.  If the default isn't
working, the app user knows what to change the value to, as well as, which
property needs to be set.



Back to the top