hlship 2004/12/21 04:52:01
Modified: framework/src/java/org/apache/hivemind/util
UtilStrings.properties UtilMessages.java
framework/src/test/org/apache/hivemind/impl
TestServicePoint.java
. status.xml
Added: framework/src/java/org/apache/hivemind/util
LocalizedContextResourceFinder.java
ContextResource.java
framework/src/test/org/apache/hivemind/util
TestContextResource.java
TestLocalizedContextResourceFinder.java
Log:
Move ContextResource from Tapestry to HiveMind.
Revision Changes Path
1.5 +1 -2
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties
Index: UtilStrings.properties
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilStrings.properties,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- UtilStrings.properties 18 Jul 2004 00:37:11 -0000 1.4
+++ UtilStrings.properties 21 Dec 2004 12:52:01 -0000 1.5
@@ -22,6 +22,5 @@
read-failure=Unable to read property {0} of object {1}: {2}
null-object=Attempt to read or update properties of null.
unable-to-introspect=Unable to introspect properties of class {0}: {1}
-
bad-file-url=Error retrieving URL for file {0}: {1}
-
+unable-to-reference-context-path=Unable to reference context path ''{0}''.
1.8 +12 -5
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java
Index: UtilMessages.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/util/UtilMessages.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- UtilMessages.java 29 Jul 2004 13:18:51 -0000 1.7
+++ UtilMessages.java 21 Dec 2004 12:52:01 -0000 1.8
@@ -15,18 +15,19 @@
package org.apache.hivemind.util;
import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
import org.apache.hivemind.impl.MessageFormatter;
/**
* Messages for the util package.
- *
+ *
* @author Howard Lewis Ship
*/
final class UtilMessages
{
- private static final MessageFormatter _formatter =
- new MessageFormatter(UtilMessages.class, "UtilStrings");
+ private static final MessageFormatter _formatter = new
MessageFormatter(UtilMessages.class,
+ "UtilStrings");
public static String noSuchProperty(Object target, String propertyName)
{
@@ -50,7 +51,8 @@
public static String writeFailure(String propertyName, Object target,
Throwable cause)
{
- return _formatter.format("write-failure", new Object[] {
propertyName, target, cause });
+ return _formatter.format("write-failure", new Object[]
+ { propertyName, target, cause });
}
public static String noReader(String propertyName, Object target)
@@ -77,4 +79,9 @@
{
return _formatter.format("bad-file-url", path, cause);
}
-}
+
+ public static String unableToReferenceContextPath(String path,
MalformedURLException ex)
+ {
+ return _formatter.format("unable-to-reference-context-path", path,
ex);
+ }
+}
\ No newline at end of file
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/LocalizedContextResourceFinder.java
Index: LocalizedContextResourceFinder.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import java.net.MalformedURLException;
import java.util.Locale;
import javax.servlet.ServletContext;
/**
* Finds localized resources within the web application context.
* <p>
* Originally part of Tapestry 3.0.
*
* @see javax.servlet.ServletContext
* @author Howard Lewis Ship
* @since 1.1
*/
public class LocalizedContextResourceFinder
{
private ServletContext _context;
public LocalizedContextResourceFinder(ServletContext context)
{
_context = context;
}
/**
* Resolves the resource, returning a path representing the closest match
(with respect to the
* provided locale). Returns null if no match.
* <p>
* The provided path is split into a base path and a suffix (at the last
period character). The
* locale will provide different suffixes to the base path and the first
match is returned.
*/
public LocalizedResource resolve(String contextPath, Locale locale)
{
int dotx = contextPath.lastIndexOf('.');
String basePath = contextPath.substring(0, dotx);
String suffix = contextPath.substring(dotx);
LocalizedNameGenerator generator = new
LocalizedNameGenerator(basePath, locale, suffix);
while (generator.more())
{
String candidatePath = generator.next();
if (isExistingResource(candidatePath))
return new LocalizedResource(candidatePath,
generator.getCurrentLocale());
}
return null;
}
private boolean isExistingResource(String path)
{
try
{
return _context.getResource(path) != null;
}
catch (MalformedURLException ex)
{
return false;
}
}
}
1.1
jakarta-hivemind/framework/src/java/org/apache/hivemind/util/ContextResource.java
Index: ContextResource.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.Resource;
/**
* Implementation of [EMAIL PROTECTED] org.apache.hivemind.Resource}for
resources found within the web
* application context.
* <p>
* Note: moved from Tapestry. Originally part of Tapestry 3.0.
*
* @author Howard Lewis Ship
* @since 1.1
*/
public class ContextResource extends AbstractResource
{
private static final Log LOG = LogFactory.getLog(ContextResource.class);
private ServletContext _context;
public ContextResource(ServletContext context, String path)
{
this(context, path, null);
}
public ContextResource(ServletContext context, String path, Locale locale)
{
super(path, locale);
_context = context;
}
/**
* Locates the resource using [EMAIL PROTECTED]
LocalizedContextResourceFinder}and
* [EMAIL PROTECTED] ServletContext#getResource(java.lang.String)}.
*/
public Resource getLocalization(Locale locale)
{
LocalizedContextResourceFinder finder = new
LocalizedContextResourceFinder(_context);
String path = getPath();
LocalizedResource localizedResource = finder.resolve(path, locale);
if (localizedResource == null)
return null;
String localizedPath = localizedResource.getResourcePath();
Locale pathLocale = localizedResource.getResourceLocale();
if (localizedPath == null)
return null;
if (path.equals(localizedPath))
return this;
return new ContextResource(_context, localizedPath, pathLocale);
}
public URL getResourceURL()
{
try
{
return _context.getResource(getPath());
}
catch (MalformedURLException ex)
{
LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(),
ex), ex);
return null;
}
}
public String toString()
{
return "context:" + getPath();
}
public int hashCode()
{
return 4197 & getPath().hashCode();
}
protected Resource newResource(String path)
{
return new ContextResource(_context, path);
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestContextResource.java
Index: TestContextResource.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import java.net.URL;
import java.util.Locale;
import javax.servlet.ServletContext;
import org.apache.hivemind.Resource;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;
/**
* Tests for [EMAIL PROTECTED] org.apache.hivemind.util.ContextResource}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class TestContextResource extends HiveMindTestCase
{
private ServletContext newContext()
{
return (ServletContext) newMock(ServletContext.class);
}
public void testConstructor()
{
ServletContext context = newContext();
replayControls();
ContextResource r = new ContextResource(context,
"/foo/bar/baz_en.html", Locale.ENGLISH);
assertEquals("context:/foo/bar/baz_en.html", r.toString());
assertEquals("/foo/bar/baz_en.html", r.getPath());
assertEquals("baz_en.html", r.getName());
assertEquals(Locale.ENGLISH, r.getLocale());
verifyControls();
}
public void testLocalizationExists() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext context = (ServletContext) control.getMock();
context.getResource("/foo/bar/baz_en.html");
control.setReturnValue(new URL("http://foo.com"));
replayControls();
ContextResource r1 = new ContextResource(context,
"/foo/bar/baz.html");
Resource r2 = r1.getLocalization(Locale.ENGLISH);
assertEquals("/foo/bar/baz_en.html", r2.getPath());
assertEquals(Locale.ENGLISH, r2.getLocale());
verifyControls();
}
public void testLocalizationSame() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext context = (ServletContext) control.getMock();
context.getResource("/foo/bar/baz_en.html");
control.setReturnValue(null);
context.getResource("/foo/bar/baz.html");
control.setReturnValue(new URL("http://foo.com"));
replayControls();
ContextResource r1 = new ContextResource(context,
"/foo/bar/baz.html");
Resource r2 = r1.getLocalization(Locale.ENGLISH);
assertSame(r2, r1);
verifyControls();
}
public void testLocalizationMissing() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext context = (ServletContext) control.getMock();
context.getResource("/foo/bar/baz_en.html");
control.setReturnValue(null);
context.getResource("/foo/bar/baz.html");
control.setReturnValue(null);
replayControls();
ContextResource r1 = new ContextResource(context,
"/foo/bar/baz.html");
assertNull(r1.getLocalization(Locale.ENGLISH));
verifyControls();
}
public void testGetRelativeResource()
{
ServletContext context = newContext();
replayControls();
ContextResource r1 = new ContextResource(context,
"/foo/bar/baz.html");
Resource r2 = r1.getRelativeResource("baz.gif");
assertEquals("/foo/bar/baz.gif", r2.getPath());
verifyControls();
}
}
1.1
jakarta-hivemind/framework/src/test/org/apache/hivemind/util/TestLocalizedContextResourceFinder.java
Index: TestLocalizedContextResourceFinder.java
===================================================================
// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.apache.hivemind.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import javax.servlet.ServletContext;
import org.apache.hivemind.test.HiveMindTestCase;
import org.easymock.MockControl;
/**
* Test for [EMAIL PROTECTED]
org.apache.hivemind.util.LocalizedResourceFinder}.
*
* @author Howard M. Lewis Ship
* @since 1.1
*/
public class TestLocalizedContextResourceFinder extends HiveMindTestCase
{
public void testFound() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext sc = (ServletContext) control.getMock();
sc.getResource("/foo/bar/baz_en_US.html");
control.setReturnValue(null);
sc.getResource("/foo/bar/baz_en.html");
control.setReturnValue(new URL("http://foo.com"));
replayControls();
LocalizedContextResourceFinder f = new
LocalizedContextResourceFinder(sc);
LocalizedResource lr = f.resolve("/foo/bar/baz.html", Locale.US);
assertEquals("/foo/bar/baz_en.html", lr.getResourcePath());
assertEquals(Locale.ENGLISH, lr.getResourceLocale());
verifyControls();
}
public void testNotFound() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext sc = (ServletContext) control.getMock();
sc.getResource("/foo/bar/baz_en.html");
control.setReturnValue(null);
sc.getResource("/foo/bar/baz.html");
control.setReturnValue(null);
replayControls();
LocalizedContextResourceFinder f = new
LocalizedContextResourceFinder(sc);
assertNull(f.resolve("/foo/bar/baz.html", Locale.ENGLISH));
verifyControls();
}
public void testNotFoundException() throws Exception
{
MockControl control = newControl(ServletContext.class);
ServletContext sc = (ServletContext) control.getMock();
sc.getResource("/foo/bar/baz_en.html");
control.setThrowable(new MalformedURLException());
sc.getResource("/foo/bar/baz.html");
control.setThrowable(new MalformedURLException());
replayControls();
LocalizedContextResourceFinder f = new
LocalizedContextResourceFinder(sc);
assertNull(f.resolve("/foo/bar/baz.html", Locale.ENGLISH));
verifyControls();
}
}
1.2 +14 -0
jakarta-hivemind/framework/src/test/org/apache/hivemind/impl/TestServicePoint.java
Index: TestServicePoint.java
===================================================================
RCS file:
/home/cvs/jakarta-hivemind/framework/src/test/org/apache/hivemind/impl/TestServicePoint.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestServicePoint.java 19 Dec 2004 15:43:07 -0000 1.1
+++ TestServicePoint.java 21 Dec 2004 12:52:01 -0000 1.2
@@ -1,3 +1,17 @@
+// Copyright 2004 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
package org.apache.hivemind.impl;
import java.util.ArrayList;
1.86 +3 -0 jakarta-hivemind/status.xml
Index: status.xml
===================================================================
RCS file: /home/cvs/jakarta-hivemind/status.xml,v
retrieving revision 1.85
retrieving revision 1.86
diff -u -r1.85 -r1.86
--- status.xml 20 Dec 2004 15:25:23 -0000 1.85
+++ status.xml 21 Dec 2004 12:52:01 -0000 1.86
@@ -121,6 +121,9 @@
which should be used as the key in a Map of all contributions. The
BuilderFactory can inject
such configurations as List or Maps.
</action>
+ <action type="update" dev="HLS">
+ Move ContextResource from Tapestry to HiveMind.
+ </action>
</release>
<release version="1.0" date="Sep 22 2004">
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]