Results 1 to 3 of 3
  1. #1
    ruchir123 is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default How can I make this Java class work

    Hello,

    I have written following java class but it is not compiling. I am new to java and that is why I am here on this forum.

    Basically I need to create a parent child relation between two class here

    The cyberBatch class wraps around the SSLFileTransfer class. In other words former is the parent of later.

    Thanks,R
    Java Code:
    package oracle.apps.fnd.cp.request;
     
    import oracle.apps.fnd.util.*;
    import oracle.apps.fnd.cp.request.*;
    import java.io.BufferedReader;
    import java.io.IOException;
    import javax.net.ssl.SSLSocketFactory;
     
     
    public class cyberBatch implements JavaConcurrentProgram {
     
        // Optionally provide class constructor without any arguments.
        // If you provide any arguments to the class constructor then while running the program will fail.
     
     
        public void runProgram(CpContext pCpContext) {
            ReqCompletion lRC = pCpContext.getReqCompletion();
            String CompletionText = "";
        
     
        // This class is to upload files but can be expanded to download files also.
     
        public class SSLFileTransfer {
     
            Properties props =
                new Properties(); // stores properties from property file
     
            /*
       * SSLFileTransfer(): constructor
       */
     
            public SSLFileTransfer() {
            }
     
            /*
       * init(): initialization (load property file)
       *
    	 * @param propsFile		properties needed for file transfer
       */
     
            public void init(String propsFile) {
                try {
                    props.load(new BufferedInputStream(new FileInputStream(new File(propsFile))));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }
            }
     
            /*
       * usage()
       */
     
            public static void usage() {
                System.out.println("USAGE: java SSLFileTransfer <full path property file name>");
                System.exit(-1);
            }
     
            /*
       * getFactory(): get factory for authentication
       *
    	 * @throws IOException	if exception occurs
       */
     
            private SSLSocketFactory getFactory() throws IOException {
                try {
                    SSLContext ctx;
                    KeyManagerFactory kmf;
                    KeyStore ks, ks1;
                    char[] passphrase =
                        props.getProperty("passPhrase").toCharArray();
     
                    ctx = SSLContext.getInstance("TLS");
                    kmf = KeyManagerFactory.getInstance("SunX509");
                    ks = KeyStore.getInstance("PKCS12", "BC");
                    ks1 = KeyStore.getInstance("JKS");
     
                    ks.load(new FileInputStream(props.getProperty("key")),
                            passphrase);
                    ks1.load(new FileInputStream(props.getProperty("keyStore")),
                             passphrase);
     
                    kmf.init(ks, passphrase);
                    TrustManagerFactory tmf =
                        TrustManagerFactory.getInstance("SunX509");
                    tmf.init(ks1);
                    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
     
                    return ctx.getSocketFactory();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new IOException(e.getMessage());
                }
            }
     
            /*
       * getHost(): Get host from property file
       */
     
            private String getHost() {
                return props.getProperty("host", "localhost");
            }
     
            /*
       * getPort(): Get port from property file
       */
     
            private int getPort() {
                return Integer.parseInt(props.getProperty("port"));
            }
     
            /*
       * sendRequest(): Send request (file) to the server
       *
    	 * @param out		stream to send the data to the server
    	 *
    	 * @throws Exception	if an error occurs.
       */
     
            private void sendRequest(PrintWriter out) throws Exception {
                String path = props.getProperty("path");
     
                out.println("POST " + path + " HTTP/1.0");
     
                final String BOUNDARY = "7d03135102b8";
                out.println("Content-Type: multipart/form-data; boundary=" +
                            BOUNDARY);
     
                String uploadFile = props.getProperty("uploadFile");
                String authString =
                    props.getProperty("bcUserName") + ":" + props.getProperty("bcPassword");
                String encodedAuthString =
                    "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes());
                out.println("Authorization: " + encodedAuthString);
     
                final String CRLF = "\r\n";
                StringBuffer sbuf = new StringBuffer();
                sbuf.append("--" + BOUNDARY + CRLF);
                sbuf.append("Content-Disposition: form-data; name=\"upfile\"; filename=\"" +
                            uploadFile + "\"" + CRLF);
                sbuf.append("Content-Type: text/plain" + CRLF + CRLF);
     
     
                FileReader fi = new FileReader(uploadFile);
                char[] buf = new char[1024000];
                int cnt = fi.read(buf);
                sbuf.append(buf, 0, cnt);
                sbuf.append(CRLF);
                sbuf.append("--" + BOUNDARY + "--" + CRLF);
     
                int sz = sbuf.length();
                out.println("Content-Length: " + sz);
     
                out.println();
                out.println(sbuf);
                out.flush();
     
                // Make sure there were no surprises
                if (out.checkError())
                    System.out.println("SSLFileTransfer: java.io.PrintWriter error");
     
            }
     
            /*
       * readResponse(): reads response from the server
       *
    	 * @param in		stream to get the data from the server
    	 *
    	 * @throws Exception	if an error occurs.
       */
     
            private void readResponse(BufferedReader in) throws Exception {
                boolean successful = false;
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    if (inputLine.startsWith("HTTP") &&
                        inputLine.indexOf("200") >= 0)
                        successful = true;
                    System.out.println(inputLine);
                }
     
                System.out.println("UPLOAD FILE " +
                                   (successful ? "SUCCESSFUL" : "FAILED") +
                                   "!!!\n");
            }
     
            /*
       * upload(): upload file to server
       *
    	 * @throws Exception	if an error occurs.
       */
     
            public void upload() throws Exception {
                try {
                    SSLSocketFactory factory = getFactory();
                    SSLSocket socket =
                        (SSLSocket)factory.createSocket(getHost(), getPort());
     
                    PrintWriter out =
                        new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
                    BufferedReader in =
                        new BufferedReader(new InputStreamReader(socket.getInputStream()));
     
                    socket.startHandshake();
                    sendRequest(out);
                    readResponse(in);
     
                    out.close();
                    in.close();
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw e;
                }
            }
     
            /*
       * main(): main method to start file transfer
       *
    	 * @param args		command line arguments (property file, see usage())
       *
    	 * @throws Exception	if an error occurs.
       */
     
            public static void main(String[] args) throws Exception {
                if (args == null || args.length != 1)
                    usage();
     
                SSLFileTransfer fileXfer = new SSLFileTransfer();
                fileXfer.init(args[0]);
                fileXfer.upload();
            }
        }
        lRC.setCompletion(ReqCompletion.NORMAL,CompletionText) ;
    }
    }

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Cross posted with more contextual info at OTN.

    The Oracle paper referenced seems to be this one: How-to - Java Concurrent Programs (Oracle BI Publisher Blog).

    @OP: that paper talks about implementing the JavaConcurrentProgram interface: nothing about parent classes, and nothing about wrapping. If you are new to Java you might benefit from starting with a tutorial to aquaint you with its syntax, and also conceptual notions like how types are related.

    Your compiler problem is that you declare a local class (that is the name for the particular type of wrapping you attempt here), but attempt to make it public which you can't do in Java. However I fear that knowing that will be of little value unless you take the time to master Java syntax and usage.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You can't have a public class nested within a public class. You need to remove the public keyword from your SSLFileTransfer class declaration.

    It's not clear why you're nesting classes, and I would suggest that maybe you don't need to. You should be able to have your two classes side-by-side, and simply instantiate an SSLFileTransfer object in your CyberBatch class.
    Java Code:
    // package oracle.apps.fnd.cp.request; // choose a better package name -- you are not Oracle
    package com.yourcompany.yourproject;
    
    import oracle.apps.fnd.util.*;
    import oracle.apps.fnd.cp.request.*;
    
    // start your class name with a capital letter
    public class CyberBatch implements JavaConcurrentProgram {
        private SSLFileTransfer sslFileTransfer = new SSLFileTransfer();
    
       public void runProgram(CpContext pCpContext) 
       { 
            ReqCompletion lRC = pCpContext.getReqCompletion(); 
            String CompletionText = ""; 
    
            sslFileTransfer.init(propsfileName);
            sslFileTransfer.sendRequest(...);
            sslFileTransfer.readResponse(...);
      
        } 
    }
    This is not complete, as I am not familiar with either JavaConcurrentProgram or SSLFileTransfer classes, but it's the basic idea. I'm pretty sure you're following along from this page:

    How-to - Java Concurrent Programs (Oracle BI Publisher Blog)

    ...and I'm guessing you were a little confused by "Code your program logic here."

    Looks like pbrockway2 has responded in the meantime, so heed his advice too.

    -Gary-

Similar Threads

  1. Make it work !
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 09-20-2010, 09:22 AM
  2. How do i make this work What am i doing Wrong.
    By Ramaan in forum New To Java
    Replies: 2
    Last Post: 03-02-2010, 12:36 AM
  3. Make the Button Work
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 02-27-2010, 11:52 AM
  4. Hints on how to make a Java Class
    By luron31 in forum New To Java
    Replies: 11
    Last Post: 07-09-2009, 06:31 AM
  5. Can't make JTable work -- please help!!
    By cagalli83 in forum Advanced Java
    Replies: 0
    Last Post: 02-13-2008, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •