2013-03-15 82 views
0

我按照這個文件模塊5.5在這個網址「http://www.ibm.com/developerworks/mobile/worklight/getting-started.html#authentication」或ftp://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v505/Module_05_5_-_Using_Java_in_Adapters.pdf我想從工作燈適配器

,並做了同樣的我的代碼結構調用Java類是

server/java      folder name 
com.worklight.custonmode   package name 
loginfunction.java     java file inside com.worklight.custonmode package 
login        java method in class loginfunction 

和我從工作燈適配器稱爲

function loginmodules(username, passwd) { 
    return { 
     result : com.worklight.custonmode.loginfunction.login() 
    }; 
} 

當我打電話我得到這樣的錯誤作爲

response [/apps/services/api/Light/common/query] success: /*-secure- {"responseID":"2","errors":["Ecma Error: TypeError: Cannot call property login in object [JavaPackage com.worklight.custonmode.loginfunction]. It is not a function, it is \"object\". 

(C%3A%5CUsers%5CADMIN%5CworkspaceM11%5CMobileClient%5Cadapters%5CAdapter /適配器-impl.js#103) 「],」 isSuccessful 「:假,」 警告 「:[],」 信息」 :[]} *在loginfunction.java

public class loginfunction { 

public static void login() { 
    //============== Code to adapt to your own configuration =============// 
    String server = "https://ibm-f4acjqe8c6p:9443/dfd";  // Set the Public URI of your RRC server 
    String JTS_Server = "https://ibm-f4acjqe8c6p:9443/jts"; //Set the public URI of your JTS server 
    String login = "Admin";         // Set the user login 
    String password = "Admin";        // Set the associated password 
    //============== -------------------------------------- =============// 

    String rootServices = server + "/rootservices"; 
    String catalogXPath = "/rdf:Description/oslc_we:rweServiceProviders/@rdf:resource"; 
    String serviceProviderTitleXPath = "//oslc:ServiceProvider/dcterms:title"; 

    System.out.println(">> Example03: Print out the content of the Service Providers catalog"); 
    System.out.println(" - Root Services URI: "+rootServices); 
    System.out.println(" - Service Providers catalog XPath expression: "+catalogXPath); 
    System.out.println(" - Service Provider title XPath expression: "+serviceProviderTitleXPath); 
    System.out.println(" - Login: "+login); 
    System.out.println(" - Password: "+password); 

    // Setup the HttClient 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpUtils.setupLazySSLSupport(httpclient); 

    // Setup the rootServices request 
    HttpGet rootServiceDoc = new HttpGet(rootServices); 
    rootServiceDoc.addHeader("Accept", "application/rdf+xml"); 
    rootServiceDoc.addHeader("OSLC-Core-Version", "2.0"); 

    try { 
     // Request the Root Services document 
     HttpResponse rootServicesResponse = HttpUtils.sendGetForSecureDocument(
               server, rootServiceDoc, login, password, httpclient,JTS_Server); 

     if (rootServicesResponse.getStatusLine().getStatusCode() == 200) { 
      // Define the XPath evaluation environment 
      XPathFactory factory = XPathFactory.newInstance(); 
      XPath xpath = factory.newXPath(); 
      xpath.setNamespaceContext(
        new NamespaceContextMap(new String[] 
          { "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#", 
           "oslc_we","http://open-services.net/xmlns/we/1.0/"})); 

      // Parse the response body to retrieve the catalog URI 
      InputSource source = new InputSource(rootServicesResponse.getEntity().getContent()); 
      Node attribute = (Node) (xpath.evaluate(catalogXPath, source, XPathConstants.NODE)); 
      String serviceProvidersCatalog = attribute.getTextContent(); 
      rootServicesResponse.getEntity().consumeContent(); 

      // Setup the catalog request 
      HttpGet catalogDoc = new HttpGet(serviceProvidersCatalog); 
      catalogDoc.addHeader("Accept", "application/xml"); 
      catalogDoc.addHeader("OSLC-Core-Version", "2.0"); 

      // Access to the Service Providers catalog 
      HttpResponse catalogResponse = HttpUtils.sendGetForSecureDocument(
                 server, catalogDoc, login, password, httpclient,JTS_Server); 
      if (catalogResponse.getStatusLine().getStatusCode() == 200) { 
       // Define the XPath evaluation environment 
       XPath xpath2 = factory.newXPath(); 
       xpath2.setNamespaceContext(
         new NamespaceContextMap(new String[] 
           { "oslc", "http://open-services.net/ns/core#", 
            "dcterms","http://purl.org/dc/terms/"})); 

       // Parse the response body to retrieve the Service Provider 
       source = new InputSource(catalogResponse.getEntity().getContent()); 
       NodeList titleNodes = (NodeList) (xpath2.evaluate(serviceProviderTitleXPath, source, XPathConstants.NODESET)); 

       // Print out the title of each Service Provider 
       int length = titleNodes.getLength(); 
       System.out.println(">> Project Areas:"); 
       for (int i = 0; i < length; i++) { 
        System.out.println(">> \t - "+ titleNodes.item(i).getTextContent()); 
       } 
      } 
     } 
     rootServicesResponse.getEntity().consumeContent(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (XPathExpressionException e) { 
     e.printStackTrace(); 
    } catch (InvalidCredentialsException e) { 
     e.printStackTrace(); 
    } finally { 
     // Shutdown the HTTP connection 
     httpclient.getConnectionManager().shutdown(); 
    } 
} 

/

worklight.js (line 1112) 

    Procedure invocation error. Ecma Error: TypeError: Cannot call property login in object [JavaPackage com.worklight.custonmode.loginfunction]. It is not a function, it is "object". (C%3A%5CUsers%5CADMIN%5CworkspaceM11%5CMobileClient%5Cadapters%5CAdapter/Adapter-impl.js#103) 

我的登錄功能}

+0

不是一個答案,但看看下面會給你任何提示(見問題的答案:http://stackoverflow.com/questions/15392206/programmatically -modify-credential-in-worklight-adapter) – 2013-03-15 09:09:54

+0

登錄是靜態方法嗎?你可以發佈loginToRM.java嗎? – longhua 2013-03-15 09:11:17

+0

另請檢查是否有名稱爲「登錄」的字段。 – longhua 2013-03-15 09:18:42

回答

0

確保login()函數是公共的和靜態的。查看您正在使用的培訓模塊的示例代碼。

+0

我已經發布我的登錄功能以上,它是靜態的,也公開 – anilgontla 2013-03-18 11:36:34

+0

任何幫助,請提前致謝 – anilgontla 2013-03-20 05:20:58

3

我剛纔明白了這一點。每個人都在這個問題上沉默了兩個月,這很有趣。對我而言,這根本不明顯,因爲它適用於某些項目,而不適用於其他項目。機器也是如此。它可以工作在一個地方,但不是另一個地方(如果你不知道發生了什麼)。

檢查您的.project文件並確保其中包含正確的buildCommand標記。

<buildCommand> 
     <name>org.eclipse.jdt.core.javabuilder</name> 
     <arguments> 
     </arguments> 
    </buildCommand> 
    <buildCommand> 
     <name>org.eclipse.wst.jsdt.core.javascriptValidator</name> 
     <arguments> 
     </arguments> 
    </buildCommand> 
    <buildCommand> 
     <name>org.eclipse.wst.common.project.facet.core.builder</name> 
     <arguments> 
     </arguments> 
    </buildCommand> 
    <buildCommand> 
     <name>com.worklight.studio.plugin.WorklightProjectBuilder</name> 
     <arguments> 
     </arguments> 
    </buildCommand> 
    <buildCommand> 
     <name>org.eclipse.wst.validation.validationbuilder</name> 
     <arguments> 
     </arguments> 
    </buildCommand> 

更多詳情:ECMA TypeError calling Java class from Worklight adapter