2014-05-05 21 views
1

我正在創建一個新的數據庫通過JS代碼的動作按鈕,然後我將一組XPage,自定義控件等複製到新的數據庫。當我這樣做時,我想將xsp.properties的內容設置爲一組已知屬性。我不知道如何從js訪問xsp.properties文件。 我已經創建了所有設計元素的Notes視圖,並且可以看到在WEB-INF/xsp.properties下列出的三個或四個元素,但不確定要從哪個元素讀取。我真正想要做的是使新DB中的xsp.properties與我從中複製的xsp.properties相同。創建一個新的數據庫,並希望通過js設置它的xsp.properties

感謝

回答

2

您可以通過Java NAPI做到這一點。對於這一點,你必須創建一個新的Java類是這樣的:

package ch.hasselba.xpages.util; 

import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 
import java.util.Properties; 

import com.ibm.designer.domino.napi.NotesAPIException; 
import com.ibm.designer.domino.napi.NotesDatabase; 
import com.ibm.designer.domino.napi.NotesNote; 
import com.ibm.designer.domino.napi.NotesSession; 
import com.ibm.designer.domino.napi.design.FileAccess; 

public class Toolbox { 

    /** 
    * loads the properties from a file 
    * 
    * @param dbPath full path of the database 
    * @param fileName name of the file to load 
    * @return the properties object 
    */ 
    public Properties loadProperties(final String dbPath, final String fileName) { 
     try { 
      // load the file 
      InputStream inStream = getFile(dbPath, fileName); 

      // if file exists, init a properties object 
      if (inStream != null) { 
       Properties props = new Properties(); 
       props.load(inStream); 
       return props; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    /** 
    * saves a property file to a database 
    * 
    * @author Sven Hasselbach 
    * 
    * @param dbPath full path of the database 
    * @param fileName name of the file to load 
    * @param props the properties object 
    */ 
    public void saveProperties(final String dbPath, final String fileName, final Properties props) { 
     try { 
      // init Notes objects 
      NotesSession nSession = new NotesSession(); 
      NotesDatabase nDB = nSession.getDatabaseByPath(dbPath); 
      nDB.open(); 

      // store properties in byte array 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      props.store(bos, "My XSP Properties"); 

      // save the property file 
      NotesNote nFile = FileAccess.getFileByPath(nDB, fileName); 
      FileAccess.saveData(nFile, fileName, bos.toByteArray()); 

      // recycle the objects 
      nFile.recycle(); 
      nDB.recycle(); 
      nSession.recycle(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * loads a property file from a database 
    * 
    * @author Sven Hasselbach 
    * @param dbPath full path of the database 
    * @param fileName name of the file to load 
    * @return InputStream content of the file 
    */ 
    private InputStream getFile(final String dbPath, final String fileName) { 
     try { 
      // init Notes objects 
      NotesSession nSession = new NotesSession(); 
      NotesDatabase nDB = nSession.getDatabaseByPath(dbPath); 
      nDB.open(); 

      // get the file 
      NotesNote nNote = FileAccess.getFileByPath(nDB, fileName); 
      InputStream inStream = FileAccess.readFileContentAsInputStream(nNote); 

      // recycle the objects 
      nNote.recycle(); 
      nDB.recycle(); 
      nSession.recycle(); 

      return inStream; 
     } catch (NotesAPIException apiEx) { 
      apiEx.printStackTrace(); 
     } 
     return null; 
    } 

} 

要編譯這個類,你必須lwpd.domino.napi.jar添加JAR 到構建路徑。

現在你可以這樣創建一個按鈕:

<xp:button 
    value="Add property" 
    id="button1"> 
    <xp:eventHandler 
     event="onclick" 
     submit="true" 
     refreshMode="complete"> 
     <xp:this.action> 
      <![CDATA[#{javascript: 
       importPackage(ch.hasselba.xpages.util); 
       var toolbox:ch.hasselba.xpages.utils.Toolbox = new Toolbox(); 
       var props:java.util.Properties = toolbox.loadProperties(database.getFilePath(), "WEB-INF/xsp.properties"); 

       props.put("Test", "123"); 

       toolbox.saveProperties(database.getFilePath(), "WEB-INF/xsp.properties", props); 
      }]]> 
     </xp:this.action> 
    </xp:eventHandler> 
</xp:button> 
+0

這看起來應該起作用。我仍在努力讓設計元素的複製能夠正常工作(非常接近)。我有一個安裝過程,根據用戶正在執行的安裝類型,將特定設計元素複製到目標數據庫(可能已經存在或可能是新的)。我有這部分過程很好地工作,如果我去新的數據庫並手動設置屬性它能正常工作,但我想檢查,看看是否設置了特定的屬性,如果沒有設置它們。 –

1

做,這是不是抄襲周圍的設計元素,而是從模板創建數據庫的最簡單方法。創建一個模板,其中包含您創建的所有數據庫中應該相同的所有內容。給它一個模板名稱,並根據模板創建數據庫(有一個呼籲)

+0

問題是,我開發的過程中會通過我的客戶使用,不是我。需要儘可能獨立。 –

+0

沒有什麼能阻止你使用你提供的數據庫作爲模板。或者提供2個數據庫:您的代碼和模板。應該可以管理。您將添加代碼以檢查模板是否可用 – stwissel

+1

您是否也需要構建代碼?請記住,至少對xsp.properties所做的一些更改需要重新生成才能生效 –

0

用Java的眼睛來看它。

屬性文件是J2EE應用程序的標準資源,其own API。因此,根據this你可以簡單地閱讀和使用Java/SSJS寫屬性:

public void saveParamChanges() { 
     try { 
      Properties props = new Properties(); 
      props.setProperty("ServerAddress", serverAddr); 
      props.setProperty("ServerPort", ""+serverPort); 
      props.setProperty("ThreadCount", ""+threadCnt); 
      File f = new File("server.properties"); 
      OutputStream out = new FileOutputStream(f); 
      props.store(out, "This is an optional header comment string"); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

而且this snippet似乎是工作。

我幾乎可以肯定,你將需要調整安全策略......

相關問題