# org.omg.PortableServer.portable

***

**1. PortableObject Reference (IOR) Creation**

```java
import org.omg.PortableServer.portable.Delegate;
import org.omg.PortableServer.portable.IdAssignmentPolicy;
import org.omg.PortableServer.portable.ObjectImpl;

public class IORCreation {

    public static void main(String[] args) {
        // Create a delegate for the servant
        Delegate delegate = new Delegate();
        
        // Create an object implementation
        ObjectImpl objectImpl = new ObjectImpl(delegate);

        // Create an IOR with the default id assignment policy
        String ior = objectImpl.iorToString();
    }
}
```

**2. Servant Activation**

```java
import org.omg.PortableServer.portable.PortableServer;

public class ServantActivation {

    public static void main(String[] args) {
        // Obtain the PortableServer reference
        PortableServer server = PortableServer.get_implementation();
        
        // Activate the servant
        server.activate();
    }
}
```

**3. Servant Deactivation**

```java
import org.omg.PortableServer.portable.PortableServer;

public class ServantDeactivation {

    public static void main(String[] args) {
        // Obtain the PortableServer reference
        PortableServer server = PortableServer.get_implementation();
        
        // Deactivate all the servants
        server.deactivate();
    }
}
```

**4. Servant Lookup**

```java
import org.omg.PortableServer.portable.ObjectReference;

public class ServantLookup {

    public static void main(String[] args) {
        // Obtain the ObjectReference of the servant
        ObjectReference objectRef = ...;
        
        // Get the servant object
        // Note: The servant type is cast here
        MyServant servant = MyServantHelper.narrow(objectRef);
    }
}
```

**5. Servant Self-Destruction**

```java
import org.omg.PortableServer.portable.ObjectImpl;

public class ServantSelfDestruction {

    public static void main(String[] args) {
        // Obtain the ObjectImpl reference of the servant
        ObjectImpl objectImpl = ...;
        
        // Self-destruct the servant
        objectImpl.destroy();
    }
}
```

**6. Request Processing Loop**

```java
import org.omg.PortableServer.portable.POAManager;

public class RequestProcessing {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        while (true) {
            // Wait for a request
            poaManager.run();
        }
    }
}
```

**7. Request Handling (Simple)**

```java
import org.omg.PortableServer.portable.POAManager;

public class RequestHandlingSimple {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        while (true) {
            // Process a single request
            poaManager.invoke();
        }
    }
}
```

**8. Request Handling (With Parameters)**

```java
import org.omg.PortableServer.portable.POAManager;

public class RequestHandlingWithParameters {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        while (true) {
            // Process a request and get its parameters
            POAContext poaContext = poaManager.invoke();
            //...
        }
    }
}
```

**9. Request Handling (With Response)**

```java
import org.omg.PortableServer.portable.POAManager;

public class RequestHandlingWithResponse {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        while (true) {
            // Process a request and send a response
            POAContext poaContext = poaManager.invoke();
            //...
            poaContext.reply();
        }
    }
}
```

**10. Request Handling (With Exceptions)**

```java
import org.omg.PortableServer.portable.POAManager;

public class RequestHandlingWithExceptions {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        while (true) {
            try {
                // Process a request
                poaManager.invoke();
            } catch (Exception e) {
                // Handle the exception
            }
        }
    }
}
```

**11. POA Lifecycle Management**

```java
import org.omg.PortableServer.portable.POAManager;

public class POALifecycleManagement {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        // Activate the POA
        poaManager.activate();
        
        // Destroy the POA (and its servants)
        poaManager.destroy();
    }
}
```

**12. Servant Communication with Clients**

```java
import org.omg.PortableServer.portable.Servant;

public class ServantCommunication implements Servant {

    public void serve() {
        // Communicate with the client using the session context
        SessionContext context = SessionContext.getCurrent();
        //...
    }
}
```

**13. Servant Communication with Other Servants**

```java
import org.omg.PortableServer.portable.ServantManager;

public class ServantCommunicationWithOtherServants {

    public void serve() {
        // Obtain the ServantManager reference
        ServantManager servantManager = ServantManager.getCurrent();
        
        // Get the servant reference by its object ID
        Servant servant = servantManager.getServant(objectId);
        //...
    }
}
```

**14. Servant Lifecycle Management**

```java
import org.omg.PortableServer.portable.ServantRequest;
import org.omg.PortableServer.portable.Servant;

public class ServantLifecycleManagement implements Servant {

    public void serve(ServantRequest servantRequest) {
        // Initialize the servant (called when the servant is activated)
        servantRequest.servantInit();
        
        // Serve the request (called when a request is received)
        //...
        
        // Destroy the servant (called when the servant is deactivated)
        servantRequest.servantDestroy();
    }
}
```

**15. Servant Security**

```java
import org.omg.PortableServer.portable.Servant;

public class ServantSecurity implements Servant {

    public void serve() {
        // Obtain the SecurityContext reference
        SecurityContext securityContext = SecurityContext.getCurrent();
        //...
    }
}
```

**16. Servant Concurrency**

```java
import org.omg.PortableServer.portable.Servant;

public class ServantConcurrency implements Servant {

    public void serve() {
        // Synchronized method to ensure thread safety
        synchronized (this) {
            //...
        }
    }
}
```

**17. Servant Persistence**

```java
import org.omg.PortableServer.portable.Servant;
import org.omg.PortableServer.portable.ServantLocator;

public class ServantPersistence implements Servant {

    public void serve() {
        // Obtain the ServantLocator reference
        ServantLocator servantLocator = ServantLocator.getCurrent();
        
        // Register the servant for persistence
        servantLocator.exportPersistent(this);
    }
}
```

**18. Servant Transfer**

```java
import org.omg.PortableServer.portable.Servant;
import org.omg.PortableServer.portable.ServantTransferDelegate;

public class ServantTransfer implements Servant {

    public void serve() {
        // Obtain the ServantTransferDelegate reference
        ServantTransferDelegate servantTransferDelegate = ServantTransferDelegate.getCurrent();
        
        // Transfer the servant to another POA
        servantTransferDelegate.transfer(newPOA, servantId);
    }
}
```

**19. Object Adapter Creation**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAManager;

public class ObjectAdapterCreation {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        // Create an object adapter with the specified policies
        POA poa = poaManager.create_POA("MyPOA", ..., ...);
    }
}
```

**20. Object Adapter Lifecycle Management**

```java
import org.omg.PortableServer.POA;

public class ObjectAdapterLifecycleManagement {

    public static void main(String[] args) {
        // Obtain the POA reference
        POA poa = ...;
        
        // Activate the POA
        poa.activate();
        
        // Destroy the POA
        poa.destroy();
    }
}
```

**21. Object Adapter Management**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAManager;

public class ObjectAdapterManagement {

    public static void main(String[] args) {
        // Obtain the POAManager reference
        POAManager poaManager = ...;
        
        // Get the POA reference by its name
        POA poa = poaManager.find_POA("MyPOA");
    }
}
```

**22. Servant Binding**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.ServantActivator;

public class ServantBinding {

    public static void main(String[] args) {
        // Obtain the POA reference
        POA poa = ...;
        
        // Bind the servant to an object ID in the POA
        byte[] objectId = poa.activate_object(servantActivator);
    }
}
```

**23. Servant Unbinding**

```java
import org.omg.PortableServer.POA;

public class ServantUnbinding {

    public static void main(String[] args) {
        // Obtain the POA reference
        POA poa = ...;
        
        // Unbind the servant from the POA by its object ID
        poa.deactivate_object(objectId);
    }
}
```

**24. Servant Relocation**

```java
import org.omg.PortableServer.POA;

public class ServantRelocation {

    public static void main(String[] args) {
        // Obtain the POA references
        POA poaSource = ...;
        POA poaDestination = ...;
        
        // Relocate the servant from the source POA to the destination POA
        poaSource.transfer(poaDestination, objectId);
    }
}
```

**25. Servant Monitoring**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.ServantLocator;

public class ServantMonitoring {

    public static void main(String[] args) {
        // Obtain the POA reference
        POA poa = ...;
        
        // Obtain the ServantLocator reference
        ServantLocator servantLocator = poa.get_servant_manager().get_servant_locator();
        
        // Monitor the servant by registering a servant monitor
        servantLocator.register_servant_monitor(servantMonitor);
    }
}
```

**26. POA Interaction with the ORB**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAManager;
import org.omg.PortableServer.ORBInitializer;

public class POAInteractionWithORB {

    public static class MyORBInitializer implements ORBInitializer {

        public void pre_init(ORBInitInfo info) {
            // Create the POAManager
            POAManager poaManager = new POAManager(...);
            
            // Register the POAManager with the ORB
            info.add_POA_manager(poaManager);
        }
        
        public void post_init(ORBInitInfo info) {
            // Create the POA
            POA poa = poaManager.create_POA("MyPOA", ...);
        }
    }
}
```

**27. Servant Communication with Other Servants in the Same POA**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;

public class ServantCommunicationInSamePOA implements Servant {

    public void serve() {
        // Obtain the ServantManager reference
        ServantManager servantManager = ServantManager.getCurrent();
        
        // Get the servant reference by its object ID
        Servant otherServant = servantManager.getServant(otherObjectId);
        //...
    }
}
```

**28. Servant Communication with Other Servants in Different POAs**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.POAPackage.AdapterAlreadyExists;
import java.util.Properties;

public class ServantCommunicationInDifferentPOAs implements Servant {

    public void serve() {
        // Obtain the POAManager reference
        POAManager poaManager = ServantManager.getCurrent().get_POA_manager();
        
        try {
            // Create a new POA
            POA otherPOA = poaManager.create_POA("OtherPOA", ...);
            
            // Obtain the other servant reference
            Servant otherServant = otherPOA.create_reference(otherServantImpl);
            //...
        } catch (AdapterAlreadyExists e) {
            // Handle the exception
        }
    }
}
```

**29. Servant Communication with Other Processes**

```java
import org.omg.PortableServer.POA;
import org.omg.PortableServer.Servant;
import org.omg.PortableServer.POAPackage.InvalidPolicy;
import org.omg.PortableServer.POAPackage.ServantNotActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;
import org.omg.CORBA.Policy;
import org.omg.CORBA.ORB;

public class ServantCommunicationWithOtherProcesses implements Servant {

    public void serve() {
        // Obtain the POAManager reference
        POAManager poaManager = ServantManager.getCurrent().get_POA_manager();
        
        try {
            // Create a new POA with the SERVANT_RETENTION policy
            Policy[] policies = new Policy[1];
            policies[0] = poaManager.create_policy(PolicyType.SERVANT_RETENTION,
                new ServantRetentionPolicyValue(ServantRetentionPolicyValue.RETAIN));
            POA otherPOA = poaManager.create_POA("OtherPOA", policies);
            
            // Obtain the IOR of the other process
            String ior = ...;
            
            // Import the servant reference from the other process
            Servant otherServant = (Servant) ORB.init(args, null).string_to_object(ior);
            //...
        } catch (InvalidPolicy | ServantNotActive | WrongPolicy e) {
            // Handle the exception
        }
    }
}
```

**30. Servant Communication with a CORBA Client**

```java
import org.omg.PortableServer.Servant;

public class ServantCommunicationWithCORBA implements Servant {

    public void serve() {
        // Obtain the session context
        SessionContext context = SessionContext.getCurrent();
        
        // Get the client object reference
        Object clientObjectRef = context.get_client_object();
        //...
    }
}
```

**31. Servant Communication with a Java Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithJava {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant
        //...
    }
}
```

**32. Servant Communication with a C++ Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithCpp {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the CPPClient API
        //...
    }
}
```

**33. Servant Communication with a Python Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithPython {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the PythonClient API
        //...
    }
}
```

**34. Servant Communication with a JavaScript Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithJavaScript {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the JavaScriptClient API
        //...
    }
}
```

**35. Servant Communication with a Delphi Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithDelphi {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the DelphiClient API
        //...
    }
}
```

**36. Servant Communication with a C# Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithCSharp {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the CSharpClient API
        //...
    }
}
```

**37. Servant Communication with a Ruby Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithRuby {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the RubyClient API
        //...
    }
}
```

**38. Servant Communication with a Swift Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithSwift {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the SwiftClient API
        //...
    }
}
```

**39. Servant Communication with a Kotlin Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithKotlin {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the KotlinClient API
        //...
    }
}
```

**40. Servant Communication with a Rust Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithRust {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the RustClient API
        //...
    }
}
```

**41. Servant Communication with a Go Client**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithGo {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the GoClient API
        //...
    }
}
```

**42. Servant Communication with a Python Client (Async)**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithPythonAsync {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the AsyncPythonClient API
        //...
    }
}
```

**43. Servant Communication with a JavaScript Client (Async)**

```java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithJavaScriptAsync {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the AsyncJavaScriptClient API
        //...
    }
}
```

**44. Servant Communication with a C++ Client (Async)**

````java
import org.omg.CORBA.ORB;

public class ServantCommunicationWithCppAsync {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Communicate with the servant using the AsyncCppClient API
        //...
    }
}

**45. Servant Communication with a Java Client (Value-Type)**

```java
import org.omg.CORBA.ORB;
import org.omg.CORBA.Any;

public class ServantCommunicationWithJavaValueType {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Create an any value to pass to the servant
        Any any = orb.create_any();
        any.insert_string("Hello, World!");
        
        // Communicate with the servant using the any value
        //...
    }
}
````

**46. Servant Communication with a Python Client (Value-Type)**

````java
import org.omg.CORBA.ORB;
import org.omg.CORBA.Any;

public class ServantCommunicationWithPythonValueType {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Create an any value to pass to the servant
        Any any = orb.create_any();
        any.insert_string("Hello, World!");
        
        // Communicate with the servant using the any value (using the PythonClient API)
        //...
    }
}

**47. Servant Communication with a JavaScript Client (Value-Type)**

```java
import org.omg.CORBA.ORB;
import org.omg.CORBA.Any;

public class ServantCommunicationWithJavaScriptValueType {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Create an any value to pass to the servant
        Any any = orb.create_any();
        any.insert_string("Hello, World!");
        
        // Communicate with the servant using the any value (using the JavaScriptClient API)
        //...
    }
}
````

**48. Servant Communication with a C++ Client (Value-Type)**

```java
import org.omg.CORBA.ORB;
import org.omg.CORBA.Any;

public class ServantCommunicationWithCppValueType {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        // Create an any value to pass to the servant
        Any any = orb.create_any();
        any.insert_string("Hello, World!");
        
        // Communicate with the servant using the any value (using the CppClient API)
        //...
    }
}
```

**49. Servant Communication with a Java Client (Exception)**

```java
import org.omg.CORBA.ORB;
import org.omg.CORBA.SystemException;

public class ServantCommunicationWithJavaException {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        try {
            // Communicate with the servant
            //...
        } catch (SystemException e) {
            // Handle the exception
        }
    }
}
```

**50. Servant Communication with a Python Client (Exception)**

```java
import org.omg.CORBA.ORB;
import org.omg.CORBA.SystemException;

public class ServantCommunicationWithPythonException {

    public static void main(String[] args) {
        // Obtain the ORB reference
        ORB orb = ORB.init(args, null);
        
        // Get the servant reference
        Object servantObjectRef = ...;
        
        try {
            // Communicate with the servant (using the PythonClient API)
            //...
        } catch (SystemException e) {
            // Handle the exception
        }
    }
}
```
