View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.innergrid.api;
18  
19  import java.io.IOException;
20  import java.util.ResourceBundle;
21  
22  import org.apache.ws.security.WSPasswordCallback;
23  
24  import javax.security.auth.callback.Callback;
25  import javax.security.auth.callback.CallbackHandler;
26  import javax.security.auth.callback.UnsupportedCallbackException;
27  
28  /**
29   * Callback handler
30   * implementation.
31   */
32  public class PWCallback implements CallbackHandler {
33    /**
34     * Password.
35     */
36    private static String password = readPassword();
37  
38    /**
39     * Read password.
40     * @return the password
41     */
42    private static String readPassword() {
43      try {
44        ResourceBundle props = ResourceBundle.getBundle("crypto");
45        return props.getString("org.apache.ws.security.crypto.merlin.alias.password");
46      } catch (Exception ex) {
47        throw new RuntimeException(ex);
48      }
49    }
50  
51    /**
52     * @{inheritDoc}
53     * Handle for the callback.
54     */
55    public void handle(Callback[] callbacks) throws IOException,
56      UnsupportedCallbackException {
57  	/**
58  	 * Traverse all callbacks looking for
59  	 * an instance of password callback and use it.
60  	 */
61      for (int i = 0; i < callbacks.length; i++) {
62        if (callbacks[i] instanceof WSPasswordCallback) {
63          WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
64          pc.setPassword(password);
65        } else {
66          throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
67        }
68      }
69    }
70  }