⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 activator.java

📁 使用OSGi框架开发的分布式电子辞典
💻 JAVA
字号:
/* * @(#)Activator.java *  * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. *  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Library General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package cn.edu.ynu.sei.dict.web.service;import cn.edu.ynu.sei.dict.core.service.IDictEditService;import cn.edu.ynu.sei.dict.core.service.IDictQueryService;import cn.edu.ynu.sei.dict.plugin.user.service.IUserManager;import cn.edu.ynu.sei.dict.web.data.AddFriend;import cn.edu.ynu.sei.dict.web.data.AddWord;import cn.edu.ynu.sei.dict.web.data.CheckEditInfo;import cn.edu.ynu.sei.dict.web.data.DeleteFriend;import cn.edu.ynu.sei.dict.web.data.EditInfo;import cn.edu.ynu.sei.dict.web.data.GetAttentionList;import cn.edu.ynu.sei.dict.web.data.GetEditInfo;import cn.edu.ynu.sei.dict.web.data.IsLogin;import cn.edu.ynu.sei.dict.web.data.UserLogin;import cn.edu.ynu.sei.dict.web.data.UserValidator;import cn.edu.ynu.sei.dict.web.data.WordDetailReacher;import cn.edu.ynu.sei.dict.web.data.WordQueryReacher;import java.net.URL;import java.util.Hashtable;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.knopflerfish.service.log.LogRef;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.BundleException;import org.osgi.framework.ServiceEvent;import org.osgi.framework.ServiceListener;import org.osgi.framework.ServiceReference;import org.osgi.service.http.HttpContext;import org.osgi.service.http.HttpService;/** * OSGi activator for StoneAge dictionary web application. * @author 88250 * @author zy * @version 1.0.2.1, Mar 13, 2008 */public class Activator implements BundleActivator {    private IDictQueryService wordReader;    private IDictEditService  wordWriter;    private IUserManager userManager;    // This is my world    static BundleContext bc;    static LogRef log;    static final String RES_ALIAS = "/";     // the http server root    static final String RES_DIR = "/www";  // bundle resource directory     static final String SERVLET = "/servlet/";    static final String USER_VALIDATOR_SERVLET_ALIAS = SERVLET + "uservalidator"; // a small servlet    static final String USER_LOGIN_SERVLET_ALIAS = SERVLET + "userlogin";    static final String WORD_DETAIL_REACHER_ALIAS = SERVLET + "worddetail";    static final String WORD_QUERY_REACHER_ALIAS = SERVLET + "wordquery";    static final String WORD_ADDWORD_ALIAS = SERVLET + "addword";    static final String WORD_EDITWORD_ALIAS = SERVLET + "editword";    static final String WORD_CHECKEDIT_ALIAS = SERVLET + "checkedit";    static final String WORD_GETEDITINFO_ALIAS = SERVLET + "geteditinfo";    static final String WORD_GETATTENLIST_ALIAS = SERVLET + "getattenlist";    static final String USER_ADDFRIEND_ALIAS = SERVLET + "addfriend";    static final String USER_DELETEFRIEND_ALIAS = SERVLET + "deletefriend";    static final String USER_ISLOGIN_ALIAS = SERVLET + "islogin";    Hashtable registrations = new Hashtable();    public void start(BundleContext bundleContext) throws BundleException {        ServiceReference sr = bundleContext.getServiceReference(IDictQueryService.class.getName());        wordReader = (IDictQueryService) bundleContext.getService(sr);        sr = bundleContext.getServiceReference(IDictEditService.class.getName());        wordWriter = (IDictEditService) bundleContext.getService(sr);                sr = bundleContext.getServiceReference(IUserManager.class.getName());        userManager = (IUserManager) bundleContext.getService(sr);        this.bc = bundleContext;        this.log = new LogRef(bundleContext);        ServiceListener listener = new ServiceListener() {            public void serviceChanged(ServiceEvent ev) {                ServiceReference sr = ev.getServiceReference();                switch (ev.getType()) {                    case ServiceEvent.REGISTERED:                        setRoot(sr);                        break;                    case ServiceEvent.UNREGISTERING:                        unsetRoot(sr);                        break;                }            }        };        String filter = "(objectclass=" + HttpService.class.getName() + ")";        try {            bundleContext.addServiceListener(listener, filter);            ServiceReference[] srl = bundleContext.getServiceReferences(null, filter);            for (int i = 0; srl != null && i < srl.length; i++) {                listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,                        srl[i]));            }        } catch (Exception e) {            log.error("Failed to set up listener for http service", e);        }    }    public void stop(BundleContext bc) throws BundleException {    }    void setRoot(ServiceReference sr) {        if (registrations.containsKey(sr)) {            return; // already done        }        log.info("set root for " + sr);        HttpService http = (HttpService) bc.getService(sr);        HttpContext context = new HttpContext() {            public boolean handleSecurity(HttpServletRequest request,                    HttpServletResponse response)                    throws java.io.IOException {                return true;            }            public URL getResource(String name) {                // when registering the root, it seems                // like we get no separator before the file.                // Is that a bug?? Code below is a workaround                      if (name.startsWith(RES_DIR) &&                        name.length() > RES_DIR.length() &&                        '/' != name.charAt(RES_DIR.length())) {                    name = RES_DIR + "/" + name.substring(RES_DIR.length());                }                // default to index.html                if (name.equals(RES_DIR)) {                    name = "/www/index2.html"; // XXX default homepage                }                // and send the plain file                URL url = getClass().getResource(name);                return url;            }            public String getMimeType(String reqEntry) {                return null; // server decides type            }        };        try {            http.registerResources(RES_ALIAS, RES_DIR, context);            http.registerServlet(USER_VALIDATOR_SERVLET_ALIAS, new UserValidator(sr,userManager),                    new Hashtable(), context);            http.registerServlet(USER_LOGIN_SERVLET_ALIAS, new UserLogin(userManager), new Hashtable(), context);            http.registerServlet(WORD_DETAIL_REACHER_ALIAS,                    new WordDetailReacher(sr, wordReader), new Hashtable(), context);              http.registerServlet(WORD_QUERY_REACHER_ALIAS, new WordQueryReacher(sr,wordReader),                    new Hashtable(), context);              http.registerServlet(WORD_ADDWORD_ALIAS, new AddWord(wordReader,wordWriter,userManager),                    new Hashtable(), context);              http.registerServlet(this.WORD_EDITWORD_ALIAS, new EditInfo(wordReader,wordWriter,userManager),                    new Hashtable(), context);              http.registerServlet(this.WORD_CHECKEDIT_ALIAS, new CheckEditInfo(),                    new Hashtable(), context);              http.registerServlet(this.WORD_GETEDITINFO_ALIAS, new GetEditInfo(),                    new Hashtable(), context);              http.registerServlet(this.WORD_GETATTENLIST_ALIAS, new GetAttentionList(userManager),                    new Hashtable(), context);              http.registerServlet(this.USER_ADDFRIEND_ALIAS, new AddFriend(userManager),                    new Hashtable(), context);              http.registerServlet(this.USER_DELETEFRIEND_ALIAS, new DeleteFriend(userManager),                    new Hashtable(), context);              http.registerServlet(this.USER_ISLOGIN_ALIAS, new IsLogin(),                    new Hashtable(), context);            registrations.put(sr, context);        } catch (Exception e) {            log.error("Failed to register resource", e);        }    }    void unsetRoot(ServiceReference sr) {        if (!registrations.containsKey(sr)) {            return; // nothing to do        }        log.info("unset root for " + sr);        HttpService http = (HttpService) bc.getService(sr);        if (http != null) {            http.unregister(RES_ALIAS);            http.unregister(USER_VALIDATOR_SERVLET_ALIAS);            bc.ungetService(sr);        }        registrations.remove(sr);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -