package org.csstudio.archive.channelarchiver;

import org.apache.xmlrpc.XmlRpcClient;
import org.csstudio.archive.ArchiveInfo;
import org.csstudio.archive.ArchiveValues;
import org.csstudio.archive.NameInfo;
import org.csstudio.platform.data.ITimestamp;

/** Main access point to the ChannelArchiver network data server.
 *  @author Kay Kasemir
 *  @author Blaz Lipuscek
 */
public class ArchiveServer extends org.csstudio.archive.ArchiveServer
{
    private final String url;
	private final XmlRpcClient xmlrpc;
	private final ServerInfoRequest server_info_request;
	private final ArchivesRequest archives_request;
		
    /** Connect to a ChannelArchiver's network data server.
     *  
     * @param url_text For example
     *        "xnds://my_server.org/archive/cgi/ArchiveDataServer.cgi"
     * @throws Exception
     */
    @SuppressWarnings("nls")
    public ArchiveServer(String url_text) throws Exception
    {
        url = url_text;
        
        // Patch 'xdns' into http, but keep the 'official' URL
        // as received.
        if (url_text.startsWith("xnds://"))
            url_text = "http://" + url_text.substring(7);
        // Create client
        xmlrpc = new XmlRpcClient(url_text);
        
        // Get server info
        server_info_request = new ServerInfoRequest();
        server_info_request.read(xmlrpc);
        
        // .. and archive keys
        archives_request = new ArchivesRequest();
        archives_request.read(xmlrpc);
    }
    
    /** {@inheritDoc} */
    @Override
    public String getServerName() 
    {
    	return "Channel Archiver"; //$NON-NLS-1$
    }

    /** {@inheritDoc} */
    @Override
    public String getURL()
    {
        return url;
    }

    /** {@inheritDoc} */
    @Override
    public String getDescription()
    {
        return server_info_request.getDescription();
    }

    /** {@inheritDoc} */
    @Override
    public int getVersion()
    {
        return server_info_request.getVersion();
    }
    
    /** {@inheritDoc} */
    @Override
    public String[] getRequestTypes()
    {
        return server_info_request.getRequestTypes();
    }
    
    /** Helper for locating a request code by name.
     *  <p> 
     * @param request_name For example: GET_RAW.
     * @return The 'request_type' ID for a given request type string.
     * @throws Exception when asking for unsupported request type.
     * @see #getRequestTypes()
     */
    @SuppressWarnings("nls")
    int getRequestCode(String request_name) throws Exception
    {
        final String request_types[] = getRequestTypes();
        for (int i=0; i<request_types.length; ++i)
            if (request_types[i].equalsIgnoreCase(request_name)) // add  IgnoreCase Albert
                return i;
        throw new Exception("Unsupported request type '" + request_name + "'");
    }
    
    /** @return Severity for an EPICS severity code. */
    SeverityImpl getSeverity(int severity)
    {
        return server_info_request.getSeverity(severity);
    }

    /** @return EPICS/ChannelArchiver status string for given code */
    String getStatus(SeverityImpl severity, int status)
    {
        if (severity.statusIsText())
        {
            final String[] status_strings = server_info_request.getStatusStrings();
            if (status >= 0  &&  status < status_strings.length)
                return status_strings[status];
            // else: Fall through...
        }
        // return the number as a string
        return Integer.toString(status);            
    }
    
	/** Get the archives under this server.
     *  <p>
     *  This implementation retrieves the list on connection,
     *  so this method returns immediately, but
     *  in general, that might not be the case.
     *  @see org.csstudio.archiveArchiveServer#getArchiveInfos()
     */
    @Override
    public final ArchiveInfo[] getArchiveInfos()
	{
		return archives_request.getArchiveInfos();
	}
 	
    /** {@inheritDoc} */
    @Override
	public final NameInfo[] getNames(int key, String pattern) throws Exception
	{
		NamesRequest infos = new NamesRequest(key, pattern);
		infos.read(xmlrpc);
		return infos.getNameInfos();
	}

    /** {@inheritDoc} */
    @Override
	public ArchiveValues[] getSamples(int key, String[] names,
			ITimestamp start, ITimestamp end,
            String request_type, Object request_parms[]) 
        throws Exception
	{
        final int request_code = getRequestCode(request_type);
		ValuesRequest values = new ValuesRequest(this,
				key, names, start, end, request_code, request_parms);
		values.read(xmlrpc);
		return values.getArchivedSamples();
	}
}

@@ -40,16 +40,55 @@
 hiver;%0A%0A
+import java.net.UnknownHostException;%0A%0A
 import o
@@ -945,32 +945,58 @@
 Exception%0A    %7B%0A
+        try%0A        %7B%0A    
         url = ur
@@ -1003,33 +1003,41 @@
 l_text;%0A        
+  
-%0A
+  %0A    
         // Patch
@@ -1091,16 +1091,20 @@
         
+    
 // as re
@@ -1111,16 +1111,20 @@
 ceived.%0A
+    
         
@@ -1171,16 +1171,20 @@
         
+    
 url_text
@@ -1229,16 +1229,20 @@
         
+   
+ 
 // Creat
@@ -1250,16 +1250,20 @@
  client%0A
+    
         
@@ -1299,33 +1299,41 @@
 _text);%0A        
+  
-%0A
+  %0A    
         // Get s
@@ -1343,16 +1343,20 @@
 er info%0A
+    
         
@@ -1394,32 +1394,36 @@
 rInfoRequest();%0A
+    
         server_i
@@ -1448,33 +1448,41 @@
 mlrpc);%0A        
-%0A
+    %0A    
         // .. an
@@ -1496,16 +1496,20 @@
 ve keys%0A
+    
         
@@ -1550,32 +1550,36 @@
 uest();%0A        
+  
+  
 archives_request
@@ -1589,24 +1589,162 @@
 ad(xmlrpc);%0A
+        %7D%0A        catch (UnknownHostException ex)%0A        %7B%0A            throw new Exception(%22Unknown host in URL %22 + url_text);%0A        %7D%0A
     %7D%0A    %0A 

Rephrase unknown host exception so that it's more obvious to end user