Showing posts with label Surya Arfandi. Show all posts
Showing posts with label Surya Arfandi. Show all posts

Sunday, July 1, 2018

How to create a method in scala


In this tutorial ill be showing you how to create a method using Scala.

The requirements are:
-Eclipse
-Latest Version of IDE

1) Once you have to install Eclipse in your machine, you ill then required to download "Scala IDE for eclipse" and "Scala worksheet".

To get this you need to open: Eclipse > Help > Install New Software as shown in the picture below
2) Afterward, a popup windows will open as shown below:
you then need to add this URL in : http://download.scala-ide.org/sdk/lithium/e47/scala212/stable/site
once the URL, has been added you need to checklist "Scala IDE for eclipse" and "Scala Worksheet" and click on next and finish.
3)When the scala has been added, the user need to create a new scala project.
once you click on a new project it will show you a popup and ask the user to name the project.
it will then create 3 items inside the project, right click on "src" and click on New > Scala WorkSheet and it will ask you to give a name for your project.

4) When a blank worksheet has been created, the user must create an object first.
in this example i created an object class and the attributes that i fill in are "carType", "cost" and "color".
i then created a method called greater, which take a parameter (c2) and define the type (which is a type car) and it return as a boolean and compares the cost of the first value to the second value "c2.cost"

I then created 2 objects which is "c1" and "c2" and gives a different values for each
After creating the objects, i would like to compare the prices between them.
i.e 100000 is greater than 3000000 shows that the result gave a false answer.

How to get and insert user LDAP using Java

Before inserting user into LDAP, we must understand why we have to have user in the first place.  The reason on why we have to have user is because this user will be used as admin user for OID-OAM integration.

When creating a user, the attributes and the objectclass should be added. After, we can then proceed to adding a user into OID.

Creating the Attributes Interface - Schema > Attributes > Create new LDAP Attributes Type

When creating the attributes, this is the interface that will pop-up on the screen. you can fill in the necessary values in.

Creating the ObjectClass Interface - Schema > Object Classes > Create new LDAP Object Classes Type

This is the interface when creating an Object class. The purpose of an object class is to group the attributes together into one. For example, attributes can be "name" and "email", and the object class can be name as "Personal Information".
So far, i can only manage to add the attributes and the object classes using ODSM (Oracle Directory Service Manager) and cannot be added through java.

Once the attributes and the objectclass has been added, we can use java to add the user.
1) First in order to create a user you must connect to the ODMS. You must provide the IP Address, username and the password in order to connect.

i.e
        Hashtable<String, Object> env = new Hashtable<String, Object>(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://10.0.63.96:3060");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=orcladmin");
        env.put(Context.SECURITY_CREDENTIALS, "TelkomSDP2017");
        InitialDirContext ctx = new InitialDirContext(env);
2) Afterward, we have to create an object "BasicAttributes" in order to create a multiple of attributes.
     BasicAttributes attrs = new BasicAttributes();

3) We then proceed to add the object class.
i.e
     Attribute classes = new BasicAttribute("objectclass");
     classes.add("personinfo");

4) After that, add the objectClass attributes to the attributes
i.e 
   attrs.put(classes);

5) Store the other attributes in the attribute set
i.e 
            attrs.put("cn", "test");
            attrs.put("namaexample", "nostra");
            attrs.put("emailexample", "nostra@nostra.com");

6) Add the new entry to the directory server, this will add the user in and also with the attributes and the object class

i.e 
   ctx.createSubcontext("ldap://10.0.63.96:3060/cn=ariexample,cn=Users,dc=telkom,dc=co,dc=id",       attrs);
            System.out.println("User has successfully been created");
RESULT:
The two result shows 3 different type of output.

1)

The 1st output come out from using apache directory studio
2)
The 2nd output comes from using the ODMS, by using this it shows all the values and their attributes.
3)
The 3rd option is by using java. here are steps on how to display the user using java output
1) First in order to display the user you must connect to the ODMS. You must provide the IP Address, username and the password in order to connect.

i.e
        Hashtable<String, Object> env = new Hashtable<String, Object>(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://10.0.63.96:3060");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=orcladmin");
        env.put(Context.SECURITY_CREDENTIALS, "TelkomSDP2017");
2) we then have to define which user that would like to be shown.
i.e
       DirContext context = new InitialDirContext(env);
            Attributes attributes =                                        context.getAttributes("cn=ariexample,cn=Users,dc=telkom,dc=co,dc=id");

            displayAttributes(attributes);

3) Afterwards, we get all the attributes and their values from the user and display in the output console.
i.e
   public static void displayAttributes(Attributes attributes) {
        if (attributes != null) {
            try {
                for (NamingEnumeration e = attributes.getAll(); e.hasMore();) {
                   
                    Attribute attr = (Attribute) e.next();
                    System.out.println("Attribute name: " + attr.getID());

                    for (NamingEnumeration n = attr.getAll(); n.hasMore(); 
                            System.out.println("value: " + n.next()));
                }
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    }