2012-09-14 71 views
0

擴展一個類,這不是我想要寫一個custom authenticationxwiki如何在我的項目

我有了寫有一個認證機制的一個項目,如果有人改掉訪問XWiki實現,我希望它通過使用XWiki實現自定義身份驗證重定向到我的項目的登錄頁面。

我試圖單獨從我的項目中編寫一個類,並將它與xwiki分開,將它打包爲一個jar,並將其放在xwiki項目的xwiki WEB-INF/lib文件夾中,因爲這就是說明爲xwiki定製認證。

的問題是,類我需要寫一些進口的XWiki實現類。如果我正在用這些導入編寫一個單獨的類,它將無法正確編譯。

我應該怎樣創建一個擴展一些XWiki實現類的類,如果它是需要進入XWiki實現的WEB-INF/lib文件夾一個單獨的類?

下面是有人張貼https://www.box.com/shared/9kamt5d9c5一個例子。

簡單的例子

package my.xwiki.custom.authentication; 

import java.io.*; 
import com.xpn.xwiki.XWikiContext; 
import com.xpn.xwiki.XWikiException; 
import com.xpn.xwiki.user.api.XWikiUser; 
import com.xpn.xwiki.user.impl.xiki.XWikiAuthServiceImpl; 


public class XWikiSSOImpl extends XWikiAuthServiceImpl { 

    @Override 
    public XWikiUser checkAuth(XWikiContext context) throws XWikiException { 
     System.out.println("we actually in checkAuth?"); 
    } 
} 

當我將jar文件添加到我的XWiki實現並運行它,我得到以下錯誤:

Detailed information: 

    Error number 0 in 11: Uncaught exception 
Wrapped Exception: Unresolved compilation problems: 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    XWikiAuthServiceImpl cannot be resolved to a type 
    XWikiUser cannot be resolved to a type 
    XWikiContext cannot be resolved to a type 
    XWikiException cannot be resolved to a type 

com.xpn.xwiki.XWikiException: Error number 0 in 11: Uncaught exception 
Wrapped Exception: Unresolved compilation problems: 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    XWikiAuthServiceImpl cannot be resolved to a type 
    XWikiUser cannot be resolved to a type 
    XWikiContext cannot be resolved to a type 
    XWikiException cannot be resolved to a type 

    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:254) 
    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:116) 
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) 
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) 
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) 
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at com.xpn.xwiki.web.ActionFilter.doFilter(ActionFilter.java:120) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.xwiki.wysiwyg.server.filter.ConversionFilter.doFilter(ConversionFilter.java:144) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at com.xpn.xwiki.plugin.webdav.XWikiDavFilter.doFilter(XWikiDavFilter.java:66) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.xwiki.container.servlet.filters.internal.SavedRequestRestorerFilter.doFilter(SavedRequestRestorerFilter.java:208) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.xwiki.container.servlet.filters.internal.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:111) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: java.lang.Error: Unresolved compilation problems: 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    The import com.xpn cannot be resolved 
    XWikiAuthServiceImpl cannot be resolved to a type 
    XWikiUser cannot be resolved to a type 
    XWikiContext cannot be resolved to a type 
    XWikiException cannot be resolved to a type 

    at my.xwiki.custom.authentication.XWikiSSOImpl <init>(XWikiSSOImpl.java:4) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at java.lang.Class.newInstance0(Class.java:355) 
    at java.lang.Class.newInstance(Class.java:308) 
    at com.xpn.xwiki.XWiki.getAuthService(XWiki.java:5288) 
    at com.xpn.xwiki.XWiki.checkAuth(XWiki.java:4090) 
    at com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl.checkAccess(XWikiRightServiceImpl.java:205) 
    at com.xpn.xwiki.XWiki.checkAccess(XWiki.java:4103) 
    at com.xpn.xwiki.XWiki.prepareDocuments(XWiki.java:5128) 
    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:179) 
    ... 34 more 
+4

顯示編譯錯誤請 –

+0

是你擴展的基類,並且要導入的類,它們都是在classpath /編譯路徑?如果他們不是,我希望他們失敗。 – corsiKa

+0

請添加代碼片段,突出顯示錯誤 – Chris

回答

2

基於鏈路上提供你似乎沒有丟失任何進口,假設你沒有那麼任何明顯的語法錯誤,它指向你的開發環境。一個典型的解決方案是使用包的路徑設置CLASSPATH變量,但在我的經驗中容易出錯。

如果您使用Eclipse,請右鍵單擊您的項目名稱並選擇屬性。在屬性對話框中,點擊左側的'Java Build Path'。在'Java Build Path'屬性中,單擊Libraries選項卡。你想點擊'Add a External Jar ...'按鈕,你將在其中指定xwiki jar的路徑。

This is what my included jars look on my project...

+0

好的,我下載了xwiki.war文件。 WEB-INF/lib文件夾中有很多xwiki jar文件。我如何知道哪些文件與我正在導入的文件相對應? – Catfish

+0

在這種情況下,您可能需要從war文件中提取所需的.class文件。 war文件與jar類似,但內部具有不同的目錄結構。兩者都是zip文件。下載7zip,用它打開戰爭,尋找保存'com.xpn.xwiki'路徑的根目錄並將其複製到本地驅動器。您現在可以將這個新目錄加入jar目錄,或者將其作爲項目的一部分包含在Class Folder中。 **注意:**一些供應商會要求您獲得許可或許可來執行此操作(即Oracle)但是使用開源將不會有任何問題(即Apache) –

+0

我只是將所有jar文件複製到了我的構建路徑然後突出顯示一條導入線並做了一個「公開聲明」,並告訴你它來自哪裏。然後我只刪除了所有的構建路徑,除了我需要的路徑。 – Catfish

1

要編譯類,編譯器需要的定義所有引用的類(包括超類)。它可以使用已經編譯過的類(如.class文件或JAR文件),或者從源代碼編譯這些類。既然你不想編譯xwiki類,你會想做前者。

因此您需要將包含這些類的jar文件添加到您的類路徑中。如何做到這一點取決於你如何調用編譯器或你使用的IDE。在eclipse中,您可以右鍵單擊項目 - > Build Path - > Add External Archives。使用javac,您可以使用-cp選項。

+0

我正在使用eclipse。我認爲你的意思是「添加外部類文件夾」? – Catfish

+0

上下文菜單中沒有稱爲「添加外部類文件夾」的選項。您是否在上下文菜單中選擇了「配置構建路徑」?在該彈出窗口中,它將是「添加外部JAR」以添加帶有類的JAR文件。如果你有一個包含類文件的目錄,即一個解壓縮的JAR文件,那麼「外部類文件夾」就是。 – meriton