I was hacking around the other day and was thinking about a single
util class (below). In this case, I implemented a 'get system
property' and a 'get system property, load class name for value,
construct it'.
This would then be used like this.
PrivilegedActions.getSystemProperty(pn, null);
or with a static import
getSystemProperty(pn, null);
-- quick util class for privileged actions --
public final class PrivilegedActions {
private static class GetSystemPropertyAction implements
PrivilegedAction<String> {
private final String propertyName;
private final String defaultValue;
private GetSystemPropertyAction(String propertyName, String
defaultValue) {
super();
assert propertyName != null && propertyName.length() > 0;
this.propertyName = propertyName;
this.defaultValue = defaultValue;
}
public String run() {
return System.getProperty(propertyName, defaultValue);
}
}
private static final class ConstructClassFromSystemProperty<T>
implements PrivilegedAction<T> {
private final String propertyName;
private ConstructClassFromSystemProperty(String propertyName) {
super();
assert propertyName != null && propertyName.length() > 0;
this.propertyName = propertyName;
}
public T run() {
final String className = System.getProperty(propertyName);
if (className == null) {
return null;
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = ClassLoader.getSystemClassLoader();
}
try {
@SuppressWarnings("unchecked")
final Class<T> clazz = (Class<T>)
Class.forName(className, true, cl);
return clazz.newInstance();
} catch (Exception e) {
return null;
}
}
}
public static String getSystemProperty(String propertyName, String
defaultValue) {
return doPrivileged(new GetSystemPropertyAction(propertyName,
defaultValue));
}
public static <T> T constructClassFromSystemProperty(String propertyName) {
return doPrivileged(new
ConstructClassFromSystemProperty<T>(propertyName));
}
private PrivilegedActions() {
}
}
On Wed, Dec 24, 2008 at 1:12 AM, Alexei Fedotov
<[email protected]> wrote:
> Nathan,
>
> Do you think that splitting the PriviAction class into four classes
> (GetSystemPropertyPriviAction, GetSecurityPolicyPriviAction,
> SetAccessiblePriviAction, GetSecurityPropertyPriviAction) would help
> clearing the mess of the initial combined class?
>
> Thanks!
>
>
> On Wed, Dec 24, 2008 at 2:46 AM, Nathan Beyer <[email protected]> wrote:
>> I meant that it's a logical mess - it does a number of different
>> things and it's not what I would call elegant code.
>>
>> If a more central and common utility is desired, then I'd suggest
>> creating one from scratch to replace PriviAction.
>>
>> -Nathan
>>
>> On Tue, Dec 23, 2008 at 3:51 AM, Alexei Fedotov
>> <[email protected]> wrote:
>>> Well, I would say it was pretty well formatted.
>>>
>>> The thing I don't like about this class is a mess of different
>>> security applications which cannot be deducted from naming. The class
>>> would be easier to understand if split into four appropriately named
>>> actions. This would also help renaming arg1, arg2 fields into
>>> something readable.
>>>
>>>
>>>
>>> On Tue, Dec 23, 2008 at 12:42 PM, Kevin Zhou <[email protected]> wrote:
>>>> Is this PriviAction really an ugly class?
>>>>
>>>
>>>
>>>
>>> --
>>> С уважением,
>>> Алексей Федотов,
>>> ЗАО «Телеком Экспресс»
>>>
>>
>
>
>
> --
> С уважением,
> Алексей Федотов,
> ЗАО «Телеком Экспресс»
>