리소스와 클래스(Resource/Class) 의 실제 위치 찾기 JSP

의외로 쓸일이 많다.
자바 클래스보다는 JSP로 쓸일이 많아서 JSP로 만들어 둔다. JSP 이름은 "resource.jsp"이다.

<%@page contentType="text/html;charset=euc-kr" %>
<html>
    <head><title>JSP Page</title></head>
     <body>
    <%
    request.setCharacterEncoding("euc-kr");

    String _resource = request.getParameter("resource");
    String _type = request.getParameter("type");
    %>
    <form action="resource.jsp" method="get">
        리소스 풀 네임 :
            <input name="resource" type="text" size="40" value="<%= _resource != null ? _resource : "" %>"><br>
        <input type="radio" name="type" value="class" checked="true">클래스
        <input type="radio" name="type" value="file">파일<br>
        클래스는 "java.lang.String" 과 같이 쓰고, 파일은 "/resources/ApplicationMessages.properties"와 같은
        형식으로 기입한다.<br>
        <input type="submit">
    </form>
    
    <%
        String path = null;
        
        if (_resource != null) {
            ResourceLocation rl = new ResourceLocation();

            if ("class".equals(_type)) {
                path = rl.getResourceURL(_resource, true);
            } else {
                path = rl.getResourceURL(_resource, false);
            }
    %>
    * 요청 리소스 : <%= _resource %><br>
    * 요청 타입 : <%= _type %><br>
    * 실제 리소스 경로 : <%= path %>
    <%
        }
    %>
    </body>
</html>

<%!
/**
 * <p>
 * Java Class 혹은 리소스(클래스패스 내에 있는 파일)의 위치를 정확히 알려준다.
 * </p>
 * 
 * @author Son KwonNam(kwon37xi@yahoo.co.kr)
 */
private static class ResourceLocation {

    /**
     * 패키지명을 포함한 클래스 풀 네임을 리소스 경로(/package/classname.class)로 바꿔준다.
     * 
     * @param classFullName
     *            클래스 풀 네임(package.classname)
     * @return 리소스 경로 (/package/classname.class)
     */
    public String classFullNameToResourcePath(String classFullName) {
        String resourcePath = classFullName.replace('.', '/').trim();
        resourcePath = "/" + resourcePath + ".class";
        return resourcePath;
    }

    /**
     * 클래스 풀 네임을 받아서 실제 파일이 위치한 URL을 리턴한다.
     * 
     * @param classFullName
     *            클래스의 풀 네임
     * @return 클래스 파일의 URL
     */
    public String getResourceURL(String classFullName) {
        return getResourceURL(classFullName, true);
    }

    /**
     * 클래스 혹은 리소스의 이름을 받아서 실제 파일이 위치한 URL을 리턴한다.
     * 
     * @param resource
     *            리소스 이름
     * @param isClass
     *            클래스인지 여부
     * @return 리소스의 URL
     */
    public String getResourceURL(String resource, boolean isClass) {
        String refinedResource = null;

        // "/dir1/dir2/resource.ext" 형태로 바꾼다.
        if (isClass) {
            refinedResource = classFullNameToResourcePath(resource);
        } else if (!resource.startsWith("/")) {
            refinedResource = "/" + resource.trim();
        } else {
            refinedResource = resource.trim();
        }

        System.out.println("검색할 리소스 : " + refinedResource);

        java.net.URL resourceUrl = ResourceLocation.class
                .getResource(refinedResource);

        if (resourceUrl == null) {
            return null;
        }

        return resourceUrl.getFile();
    }
}
%>

by 권남 | 2005/03/10 09:24 | 프로그래밍 | 트랙백(1) | 덧글(0)

트랙백 주소 : http://kwon37xi.egloos.com/tb/1443423
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Tracked from 오늘도 날자 at 2005/07/14 10:43

제목 : 리소스와 클래스 실제 위치 찾기
리소스와 클래스의 실제 위치 찾기 JSP 많이 요긴할것 같은 동적 리소스 찾아내기. ^^...more

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶