posts - 56, comments - 54, trackbacks - 0, articles - 4
             ::  ::  :: 聯(lián)系 :: 聚合  :: 管理

          WebLogic JNDI 使用方法簡介
           
          參考資料
          1: http://www.weblogic.com/docs/classdocs/API_jndi.html
          2: BeaHOME\wlserver6.1\samples\examples\jndi

          public static void main(String[] args) {
              // Use weblogic test JNDI
              // project include BeaHOME\wlserver6.1\lib\weblogic.jar
              String bindingkey = "UserAccount";
              Context initialContext = null;
              try {
                  // 1 Create a Properties object and set properties appropriately
                  Properties props = new Properties();
                  // Take a look at BeaHOME\wlserver6.1\samples\examples\jndi
                  // 1.1
                  props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                  // 1.2   
                  props.put(Context.PROVIDER_URL, "t3://localhost:7001");

                  // 2 Create the initial context from the properties we just created
                  initialContext = new InitialContext(props);

                  HashMap lst = new HashMap();
                  lst.put("enterprise_code", "600000");
                  lst.put("username", "S02888");
                  lst.put("password", "sysmex");

                  // Sometimes bind twice will cause NamingException  
                  //initialContext.unbind(bindingkey);
                  try {
                      // binding is copy obj's value to server
                      initialContext.bind(bindingkey, lst);
                  } catch (NameAlreadyBoundException e) {
                      initialContext.rebind(bindingkey, lst);
                  }

                  // Look up the object. copy obj's value from server
                  Object obj = initialContext.lookup(bindingkey);

                  if (bindingkey.equals("")) {
                      System.out.println("Looked up the initial context");
                  } else {
                      System.out.println(bindingkey + " is bound to: " + obj);
                  }
              } catch (NamingException e) {
                  System.out.println("NamingException msg = " + e.getMessage());
              } finally {
                  if (initialContext != null) {
                      try {
                          initialContext.close();
                      } catch (NamingException e) {
                          System.out.println("Failed to close context due to: " + e);
                      }
                  }
              }
          }
           
          用ACLs限制JNDI訪問
           
              給JNDI的訪問加入權(quán)限限制.

              1: 在 http://localhost:7001/console/ 中設(shè)置
              Security--->ACLs   Create a new ACL...
              Name=weblogic.jndi.myapp  // 這個(gè)myapp就是要限制的JNDI路徑.
              Permissions=lookup (first time)
              Permissions=modify (secend time)
              User=user1
               if there is a error system has not "modify" permission
               please add one line in filerealm.properties
               acl.modify.weblogic.admin=Administrators


              2: 代碼中訪問JNDI是要輸入用戶名 密碼

              static final String JNDI_PATH = "myapp";

              public void bindUserData(
                  String sessid,
                  String enterpriseCode,
                  String userId,
                  String password) {

                  // Use weblogic test JNDI
                  // project include BeaHOME\wlserver6.1\lib\weblogic.jar
                  String bindingkey = sessid;
                  Context initialContext = null;
                  try {
                      // 1 Create a Properties object and set properties appropriately
                      Properties props = new Properties();
                      // Take a look at BeaHOME\wlserver6.1\samples\examples\jndi
                      props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                      props.put(Context.PROVIDER_URL, "t3://localhost:7001");
                      // ユーザおよびパスワードを T3User にパッケージ化して、
                      // パスワードを確実に暗號(hào)化する
                      props.put(Context.SECURITY_CREDENTIALS, new T3User("user1", "psw1")); 
                     
                      // 2 Create the initial context from the properties we just created
                      initialContext = new InitialContext(props);

                      HashMap lst = new HashMap();
                      //lst.put("enterprise_code", "600000");
                      //lst.put("username", "S02888");
                      //lst.put("password", "sysmex");

                      lst.put("enterprise_code", enterpriseCode);
                      lst.put("username", userId);
                      lst.put("password", password);

                      try {
                          initialContext.createSubcontext(JNDI_PATH);
                          System.out.println("Subcontext 'myapp' created");
                      } catch (NameAlreadyBoundException e) {
                          // サブコンテキストがすでに存在している。
                          // 名前が同じオブジェクトにすでにバインドされている場(chǎng)合、
                          // WebLogic のコンテキストの実裝で、この例外は送出されない。
                          System.out.println("Subcontext 'myapp' already exists;" + " continuing with existing subcontext");
                      }

                      initialContext.unbind(JNDI_PATH + "." + bindingkey);
                      // bind is copy obj's value to server
                      initialContext.bind(JNDI_PATH + "." + bindingkey, lst);

                      // Look up the object. copy obj's value from server
                      Object obj = initialContext.lookup(JNDI_PATH + "." + bindingkey);

                      if (bindingkey.equals("")) {
                          System.out.println("Looked up the initial context");
                      } else {
                          System.out.println(
                              JNDI_PATH + "." + bindingkey + " is bound to: " + obj);
                      }

                  } catch (NamingException e) {
                      System.out.println("NamingException msg = " + e.getMessage());
                  } finally {
                      if (initialContext != null) {
                          try {
                              initialContext.close();
                          } catch (NamingException e) {
                              System.out.println("Failed to close context due to: " + e);
                          }
                      }
                  }
              }

          // 補(bǔ)充:遍歷所有元素
          NamingEnumeration ne = initialContext.list("."); // 用句號(hào)表示根目錄
          while (ne.hasMoreElements()){
              System.out.println(ne.next());  
          }
           
           


          評(píng)論

          # re: WebLogic JNDI 使用方法簡介  回復(fù)  更多評(píng)論   

          2011-04-26 11:54 by zcq87642231
          このやろう。 それは日本に向けのプロージェックトじゃん。
          主站蜘蛛池模板: 麻城市| 仪陇县| 太和县| 车险| 于田县| 佳木斯市| 上杭县| 合山市| 兖州市| 岑溪市| 房产| 孟津县| 新津县| 和田市| 车致| 抚宁县| 房产| 威信县| 鹤山市| 卢湾区| 南宫市| 九龙坡区| 巫山县| 泉州市| 乌兰浩特市| 建湖县| 大宁县| 湘潭县| 隆林| 大悟县| 佛学| 兴安县| 丹凤县| 大庆市| 广安市| 汉沽区| 尚志市| 九台市| 永川市| 深圳市| 玉溪市|