Can this be done with Java RMI?

Discussion in 'Mac Programming' started by LD220, Mar 4, 2010.

  1. LD220 macrumors member

    Joined:
    Jun 3, 2009
    #1
    I would like to write a program using Java RMI and my friends and I have been bouncing some ideas around. Basically we have a bunch of files on one machine, but they have the wrong file names. We were wondering if we could write and RMI server on another machine, that when called by the client will rename (or copy/rename and delete the old) the files directly on the client machine. Not sure if that makes sense, so if more explanation is needed let me know. I just was wondering if this is something that is even possible, before I attempt to write the programs.

    Thanks in advance for you help!
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Java RMI just allows you to call remote methods from an already existing Java application. If you have the files on the client already it would be pointless to call methods on a server, just integrate those methods directly into the client. Or perhaps just bundle them up into a library.

    If you called the methods on the server it would have to open a connection to the client computer and then rename the files and then close the connection.

    I'm not sure you have thought this through. Either that or your explanation is badly worded.
     
  3. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #3
    I completely agree with you that it is pointless actually. The reason we're doing it this way is because we plan to have multiple clients that want to use the renaming program as a service. It's really just going to be a project for school and we're using RMI because that's what we're learning at the moment (and if we were to program it directly it would probably be fairly trivial).

    This was just an idea that we came up with for something that we're going to integrate into a larger system and I wasn't sure that it was actually even possible to connect to the client machine and modify files on that side, so I didn't want to propose it if it wasn't. From your post though, it seems as though it is? Is that right.
     
  4. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #4
    Java Remote File Renaming

    I would like to be able to write a server program that can rename files when it's called by a client program, preferably using RMI. Basically, I want the client to be able to make a call to the server and give it the name of the file now and what the new name should be and I want the server to be able to access the file directly on the client machine and rename it. Is this possible to do with Java? If so, what would be the best way to go about it, and can it be done using RMI? Thanks for the help.
     
  5. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #6
    sorry about that. i actually thought i had posted the original on a different forum. i'm still looking for answers though. i've been googling the topic all morning and haven't really found anything specific enough, so if anyone knows how to do this i would really appreciate the help.
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #7
    If you really want to use RMI then I think you need to choose something that is better suited for it. This is not the kind of use that RMI was designed for really, it is better suited for handling distributed work loads and such like.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    How do you plan to represent the file residing on the server, in a way that any client can refer to it?

    You could use a simple relative pathname. Or just the name in a specific directory on the server. Then changing the name of the object on the client causes the server to change the filename in the specific server-resident directory.

    RMI, or any distributed objects system, is based on the premise that you have distributable objects. "Distributable" means it can be distributed to any participating machine and it will work. "Objects" means an object-oriented model of some kind.

    Frankly, renaming a remote file doesn't fit that pattern too well. First, it assumes a pre-existing thing called a file, residing on the server, which can be renamed. Assuming pre-existing things is the antithesis of "distributable". You can't distribute the assumption to any machine and have it work. If you want to model the server file-system itself (or a part thereof), then you need more than just renaming, you need a way to list things.

    Second, a file residing at a particular pathname is hardly an object-oriented model. If anything, it's something else that objects can be used to model: a container for images, a provider of text, etc. A file's pathname doesn't even have to refer to a file residing on a particular machine. It can refer to a file residing on another remote machine, which is mounted as a sharepoint, and which can be independently renamed, moved, or deleted by things entirely outside the object-oriented model.

    Suppose you invert the design, and allow the creation, listing, and renaming of objects, which are persisted by filenames, then that's an object-oriented model. The name and existence of a file represents the name and existence of the modeled object. What is it a model of? Cats and their names. Hospital patients. Fashionable spring colors. It doesn't matter. Pick something to model, then model it.

    Ultimately, deep inside the model objects, where it interfaces to the file-system, you'll have code that responds to a rename request by calling File.renameTo(). If you have a workable design and a model, that particular bit of code will naturally fall out of the rest of the design. If you don't have a design and a model, it won't.

    So first things first: figure out what your model is, and what the modeled objects represent. Next, figure out what actions can be applied to the modeled objects, and write them out as abstract methods. Next, figure out how each method performs its task, given either a local request or a remote request. Finally, turn it into RMI. Notice how much work is needed before even considering RMI. You can't skip that work, because it's what the RMI is eventually built on.
     
  8. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #9
    Thank you for the very detailed response, but a good amount of it kind of went over my head. I am not looking to make a very complicated program. The service that I am attempting to create is to be used by clients that are going to be using another program that creates hundreds of files in one place on the client machine. There will be no files on the server machine, just this remote object that will probably only have one remote method, called rename. I want the client to be able to access the object through the registry, so that it can call rename, and have the files that were previously created on the client machine, renamed on the client machine. I know how to do this if the client and server files are on the same machine, in different folders, by using the absolute path, but I do not know how to do this if the server class is on one machine and the client class on another. I'm assuming I need to set up a network accessible folder, or pass a URL of some sort, but I don't know how to do this.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    So you want to set up RMI on a remote server, that then sends the rename request back to the originating client, telling it to rename its local file?

    Is the client also running an RMI server? Because if it's not, then how is the RMI server on the remote machine going to tell the client (which is acting as its server) to do anything at all?

    Step 1: You need to think through how you'd get the remote server to rename files on the client. If you can't solve that, then nothing else can possibly work. In that case, you should probably choose a different model.

    Step 2: With the answer to Step 1, figure out what to tell the remote server in order for it to rename the file. At least 2 things come to mind: pathname (or some other representation) identifying the specific file to rename; the new name.

    Step 3: Figure out how to represent the things in Step 2 as objects which can be remotely implemented yet still work.

    Step 4: If you get this far, the rest should be more or less obvious. If you don't get this far, go back to Step 1 and choose a different model.
     
  10. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #11
    Ideally, I would like to be able to send the file path to the server and have the server call renameTo on that, but I think I need to place the files in a network accessible location and maybe use a URL or URI for that. I wrote these files quickly this morning and they run fine on my computer, but I don't know how to move the server files to a different machine and still have them rename the client files.
     

    Attached Files:

  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    That's your Step 1. You haven't done it yet. You're already writing RMI code without knowing what the code has to do.

    Forget RMI for now. Do Step 1, and figure out how the remote machine is going to do its task. Once you know how to do that, then you can work out everything else. If you can't solve Step 1, nothing else matters.


    Questions you need to consider:

    Does a URL or URI have a delete capability?

    What does have delete capability? If it's File, then what should the pathname be if it's referring to a file on a network disk?

    How would you find out what the pathname of a mounted network disk is? Consider using the 'ls' command and the 'mount' command, after a network volume is mounted.


    Your earlier thread indicated this is a homework problem, or some kind of project for school.

    Problem solving is the heart of software design. You need to think this problem through, from the viewpoint of the remote server machine, figure out how to solve it, then implement your RMI accordingly. It's not that complicated, but you do need to think it through.

    The way you're doing it now is backwards: you don't know how to solve the problem of referring to a remote file, but you're already writing RMI code. Figure out how to solve the basic problem, from the viewpoint of the remote machine, then you can worry about coding it.

    BTW, your posted code doesn't rename a local file. So I don't know what it means that they "run fine" on your local computer. Yes, they run fine, but they don't do anything even vaguely like the problem you're trying to solve.

    If you haven't gone through Sun's RMI tutorial, you probably should. It's a real example that performs a real remote task. It doesn't solve the problem of the remote machine referring to a file on a network disk, but it does do something on the remote RMI server that's returned to the client.
    http://java.sun.com/docs/books/tutorial/rmi/index.html


    ADDENDUM:
    If both computers aren't running Mac OS X, then you also need to take into account that different OSes will probably refer to networked file-systems in different ways. Getting that to work may be more difficult than you think, even after you solve it for Mac OS X.
     
  12. LD220 thread starter macrumors member

    Joined:
    Jun 3, 2009
    #13
    Thanks. This is for school, but unfortunately we are not being taught anything. We were just told to use RMI, so my issue is that I don't know if what I am trying to do is even possible. We also weren't taught anything about RMI, so yes, I did look at the RMI tutorial on Sun and I found it very helpful. Obviously, I'm pretty frustrated by the whole situation.

    First and foremost, I am really looking for a yes/no answer to whether it is even possible to rename a file on a client from a server using Java, which I think was your point. I understand that problem solving is key, but I have done hours of googling and have not found a clear yes/no. From there, if the answer is yes, I will need to figure out the best way to do it. If it's not possible, I'm just wasting my time. Thanks again.
     
  13. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #14
    The answer is YES.

    It is possible to rename files via Java. It is possible to execute Java programs on more than one machine (i.e. client and server) that communicate with each other via RMI. Therefore, it is possible to rename files via Java using server and client programs.

    The only potential issues are starting the client and/or server program with the right permission to access either the file or send/receive packets on the network port used for RMI. If you have sufficient privileges on both machines, this is definitely doable. If you don't have root (or equivalent) privileges, then it could be a roadblock depending on the level of privileges that your program does have.

    I must say I really don't understand why you need to use a client to ask a server to rename a file on the client. If you need a server to provide unique names for multiple clients (i.e. making filenames unique across all clients), then a better solution might be for the client to submit the RMI request to the server to request a new name. Then your client does the rename itself based on the name returned. That avoids the second step of server-to-client RMI.
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    It depends.

    Reasons why it depends...

    You haven't identified precisely what you're trying to do on the server. You haven't identified what OS the server is running. You haven't said whether you will assume that the server already has the remote client's disk mounted or not. If the remote client is already mounted, you haven't said what you can assume the pathname on the server will be. You haven't said what access privileges the server is using. You haven't said whether multiple independent clients will be expected to connect to the server, and perform this roundabout file-renaming on themselves.

    Those are just the questions that immediately come to mind for me. I could probably come up with more, given time.


    Without knowing what you googled, no response or advice is possible.

    In addition to googling, my immediate earlier reply gave two specific names of commands (ls, mount) you might find useful in discovering pathnames, assuming the server itself is running Mac OS X. Sometimes you have to poke around and discover things yourself, rather than searching for what's already been discovered but might not be classified under the words you're using for it.

    If the server isn't running Mac OS X (and since you haven't specified, I have no reason to assume) then you'd have to figure out what tools you can use on your prospective server to learn how network file-systems are connected, and what those pathnames might be. If that sounds like a formidable task, then re-examine your earlier estimate that roundabout renaming by RMI would be easy.

    EXERCISES:

    Suppose Mac OS X will be acting as your server. Start by ejecting with Finder all network disks or shares.

    Enter the following Terminal commands:
    Code:
    mount
    ls -l /Volumes
    
    Next, connect to one network disk or share. Enter the same two commands. Note the differences.

    Connect to a second network disk or share, without disconnecting from the first. Same two commands again. Note any emerging pattern.

    Insert a USB flash drive. Repeat the two commands. Note any changes to the pattern.

    For a hypothetical directory Foo located in /Volumes, enter this command:
    Code:
    ls -l /Volumes/Foo
    
    Does the listing have any correlation to the contents of the connected network disk, share, or flash drive named Foo?

    Given the evidence observed so far, formulate a hypothesis that explains how a network disk or share might be represented as a pathname. Describe how to distinguish these from other possible mounted volumes, such as partitions or USB flash drives.

    Create a simple stand-alone (i.e. non-RMI) Java command-line tool that you can use to explore this hypothesis, perhaps starting by doing something as simple as listing the contents of a directory in /Volumes/ and printing it to System.out with available read & write accessibility. Expand the tool to provide any further information available to Java classes.

    Note: The above exercises are just the beginning of what you have to figure out. For example, security hasn't come up at all. It would be very bad to accidentally delete things in an important directory, just because you wrote code that blindly accepted some arbitrary pathname as a remote disk that some client asked to delete a file on.


    "Possible" is ambiguous. Possible for you? Possible for me? Possible for whom? Possible in what amount of time? Given what amount of education and effort?

    Seriously, consider what you're going through right now, just trying to figure out whether the renaming is possible. All that, just to do an oddly designed self-referential file-rename that goes through a remote server and then back to itself, for no practical reason at all. Do you really need to learn how to design such complicated pointless things?

    If you need an RMI project, pick a simpler one. One that you already know how to design and implement. It can still be impractical, in the sense of serving no real purpose when applied in the real world, but at least you'll know how to do it. You expected that roundabout remote renaming via RMI was going to be a simple thing to do. Now you know better.
     

Share This Page