2013-02-04 6 views
1

'common'이라는 또 다른 Grails 플러그인을 사용하는 'foo'라는 Grails 플러그인이 있습니다.Grails Plugin에서 리소스에 액세스하기

grails.plugin.location.'common' = "../common" 

'일반적인'플러그인은 도메인 클래스뿐만 아니라 리소스 파일 (.properties의 파일, XML 템플릿, ...)를 포함한다. 이 파일들은 모두 서브 폴더에 common/grails-app/conf/에 있습니다.

제대로 작동하려면이 파일을 사용하는 '공통'플러그인에 NamespaceContext를 구현하는 클래스가 있습니다.

public class MyNamespaceContext implements NamespaceContext { 

private Map<String, String> namespaces; 

public MyNamespaceContext() { 

    final String XML_NAMESPACES_FILE = "grails-app/conf/xml/xmlNamespaces.properties"; 

    try { 
     Properties xmlNamespaces = new Properties(); 
     xmlNamespaces.load(new FileReader(XML_NAMESPACES_FILE)); 
     namespaces = new HashMap<String, String>((Map) xmlNamespaces); 
    } catch (FileNotFoundException e) { 
     throw new RuntimeException("XML namespaces file '" + XML_NAMESPACES_FILE + "' cannot be found"); 
    } catch (IOException e) { 
     throw new RuntimeException("IOException"); 
    } 
} 

... 

}

이 클래스는 XML 데코레이터로 구현 내 도메인 모델을 형성하는 '일반적인'에있는 여러 클래스에서 사용된다. 대신

public class UserXmlDecorator implements User { 

    private Document xmlDocument; 
    private XPath xPath; 
    private final String rawXml; 

    public UserXmlDecorator(String rawXml) { 
     this.rawXml = rawXml; 
     this.xmlDocument = XmlDocumentFactory.INSTANCE.buildXmlDocumentInUTF8(rawXml); 
     this.xPath = XPathFactory.newInstance().newXPath(); 
     xPath.setNamespaceContext(new MyNamespaceContext()); 
    } 

    public String getUserName() { 
     try { 
      XPathExpression userNameXPathExpr = xPath.compile("..."); 
      String userName = userNameXPathExpr.evaluate(appendixBXmlDocument); 
      return userName; 
     } catch (XPathExpressionException e) { 
      throw new RuntimeException(); 
     } 
    } 

    public String getAge() { 
     try { 
      XPathExpression ageXPathExpr = xPath.compile("..."); 
      String age = ageXPathExpr.evaluate(appendixBXmlDocument); 
      return age; 
     } catch (XPathExpressionException e) { 
      throw new RuntimeException(); 
     } 
    } 

foo는/Grails에 - 응용 프로그램에서 템플릿을 찾고 있기 때문에 내 Grails의 플러그인 'foo는'내가하는 FileNotFound 예외를 얻을 이러한 장식을 생성/conf의/XML/xmlNamespaces.properties, common/grails-app/conf/xml/xmlNamespaces.properties입니다.

나는 Grails: How to reference a resource located inside an installed plugin?을 읽었으나이 점이 도움이되지 못했습니다.

어떻게하면 해결할 수 있습니까?

+0

이것은 디자인 문제와 같습니다. 이상적으로, 일반적인 플러그인은 플러그인 foo가 호출 할 수있는 메소드를 노출해야합니다. – Rohit

+0

게시물을 업데이트하고 xml 리소스를로드하는 데 사용하는 코드를 추가하면 도움이 될 수 있습니다. – uchamp

+0

관련 코드 조각을 추가했습니다. – Will

답변

1

conf/디렉토리 대신 클래스 경로에 .properties 파일을 넣은 다음 클래스 로더를 사용하여 리소스를로드합니다.

xmlNamespaces.load(this.getClass().getClassLoader().getResourceAsStream(XML_NAMESPACES_FILE)); 
관련 문제