2010-10-15 52 views
0

我有我的一個客戶以下情形:託管服務器,更改IP和JNLP

他有一個託管服務器(1)(即http://customer.net)基本上重定向到一個地址格式爲:http:/ // app(服務器2)

IP地址經常變化(他們甚至每兩週告訴我一次)。服務器2中有一個帶有jnlp鏈接的應用程序,顯然它被配置爲從服務器2 IP下載。當服務器2的IP更改時,jnlp將被打破。

對我來說,顯而易見的選擇是獲得提供商的專用IP地址,但想知道是否有任何其他選項。

提前致謝!

回答

1

我能弄明白。基本上我有一個servlet,jnlp鏈接被觸發。我從請求中收集正確的URL並在下載之前用它更新jnlp文件。此外,我還添加了一些臨時文件,以避免只有在URL更改時纔會這樣做。

下面是相關的代碼:

 File jnlp = null; 
     File backup = null; 
     String protocol = "http://"; 
     String url = request.getRequestURL().toString(); 
     url = url.substring(url.indexOf(protocol) + protocol.length(), url.indexOf("/xinco/menu.jsp")); 
     File last = new File(getServletContext().getRealPath("/client/" + url + ".xinco")); 
     if (!last.exists()) { 
      File dir = new File(getServletContext().getRealPath("/client/")); 
      String[] list = dir.list(new ExtensionFilter(".xinco")); 

      if (list.length != 0) { 
       for (int i = 0; i < list.length; i++) { 
        new File(dir.getAbsolutePath(), list[i]).delete(); 
       } 
      } 
      try { 
       jnlp = new File(getServletContext().getRealPath("/client/XincoExplorer.jnlp")); 
       backup = new File(getServletContext().getRealPath("/client/XincoExplorer.jnlp.bak")); 
       backup.createNewFile(); 
       if (jnlp.exists()) { 
        FileChannel source = null; 
        FileChannel destination = null; 
        try { 
         source = new FileInputStream(jnlp).getChannel(); 
         destination = new FileOutputStream(backup).getChannel(); 
         destination.transferFrom(source, 0, source.size()); 
        } finally { 
         if (source != null) { 
          source.close(); 
         } 
         if (destination != null) { 
          destination.close(); 
         } 
        } 
        try { 
         StringBuilder contents = new StringBuilder(); 
         //use buffering, reading one line at a time 
         //FileReader always assumes default encoding is OK! 
         BufferedReader input = new BufferedReader(new FileReader(jnlp)); 
         try { 
          String line = null; //not declared within while loop 
         /* 
          * readLine is a bit quirky : 
          * it returns the content of a line MINUS the newline. 
          * it returns null only for the END of the stream. 
          * it returns an empty String if two newlines appear in a row. 
          */ 
          while ((line = input.readLine()) != null) { 
           if (line.contains("codebase") && !line.startsWith("<!")) { 
            String start = line.substring(0, 
              line.indexOf(protocol) + protocol.length()); 
            String end = null; 
            end = line.substring(line.indexOf("/xinco")); 
            line = start + url + end; 
           } 
           contents.append(line); 
           contents.append(System.getProperty("line.separator")); 
          } 
          //use buffering to update jnlp 
          Writer output = new BufferedWriter(new FileWriter(jnlp)); 
          try { 
           //FileWriter always assumes default encoding is OK! 
           output.write(contents.toString()); 
          } finally { 
           output.close(); 
          } 
         } finally { 
          input.close(); 
          backup.delete(); 
          last.createNewFile(); 
         } 
        } catch (IOException ex) { 
         try { 
          source = new FileInputStream(backup).getChannel(); 
          destination = new FileOutputStream(jnlp).getChannel(); 
          destination.transferFrom(source, 0, source.size()); 
          backup.delete(); 
         } finally { 
          if (source != null) { 
           source.close(); 
          } 
          if (destination != null) { 
           destination.close(); 
          } 
         } 
        } 
       } else { 
        throw new XincoException("Missing XincoExplorer.jnlp!"); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     }