@Override public void init(ServletConfig config) throws ServletException { try { ParameterContext c = new ServletContextParameterContext(config.getServletContext()); String gridId = c.getValue("langrid.node.gridId"); if (gridId == null) { logger.info("langrid.node.gridId is not set. ignore initializing domain."); return; } DaoFactory f = DaoFactory.createInstance(); DaoContext dc = f.getDaoContext(); jp.go.nict.langrid.dao.initializer.DomainInitializer.init( f, dc, gridId, c.getBoolean("dropAndCreate", false), config.getServletContext().getRealPath("/WEB-INF/init")); ProtocolDao dao = f.createProtocolDao(); Set<String> protos = new HashSet<String>( CollectionUtil.collect( dao.listAllProtocols(), new Transformer<Protocol, String>() { @Override public String transform(Protocol value) throws TransformationException { return value.getProtocolId(); } })); if (!protos.contains(Protocols.SOAP_RPCENCODED)) { dao.addProtocol(new Protocol(Protocols.SOAP_RPCENCODED)); } if (!protos.contains(Protocols.PROTOBUF_RPC)) { dao.addProtocol(new Protocol(Protocols.PROTOBUF_RPC)); } if (!protos.contains(Protocols.JSON_RPC)) { dao.addProtocol(new Protocol(Protocols.JSON_RPC)); } } catch (DaoException e) { throw new ServletException(e); } catch (IOException e) { throw new ServletException(e); } catch (SQLException e) { throw new ServletException(e); } }
private static void setupUsers(DaoFactory factory, String gridId, File baseDir) throws Exception { baseDir = new File(new File(new File(baseDir, "grids"), gridId), "users"); UserDao dao = factory.createUserDao(); for (File f : baseDir.listFiles(new RegexFileNameFilter(".*\\.json$"))) { User u = new LangridJSON(baseDir, FilenameUtil.getIdFromFileName(f)) .parse(new FileInputStream(f), User.class); u.setPassword(MessageDigestUtil.digestBySHA512(u.getPassword())); dao.addUser(u); } }
private static List<String> setupGrids(DaoFactory factory, File baseDir) throws Exception { baseDir = new File(baseDir, "grids"); GridDao gdao = factory.createGridDao(); DomainDao ddao = factory.createDomainDao(); List<String> gridIds = new ArrayList<String>(); for (File f : baseDir.listFiles(new RegexFileNameFilter(".*\\.json$"))) { Grid g = new LangridJSON(baseDir, FilenameUtil.getIdFromFileName(f)) .parse(new FileInputStream(f), Grid.class); List<Domain> domains = new ArrayList<Domain>(); for (Domain d : g.getSupportedDomains()) { domains.add(ddao.getDomain(d.getDomainId())); } g.getSupportedDomains().clear(); g.getSupportedDomains().addAll(domains); gdao.addGrid(g); gridIds.add(g.getGridId()); } return gridIds; }
public static void main(String[] args) throws Exception { System.setProperty( "jp.go.nict.langrid.dao.hibernate.LangridSessionFactory.hibernate.cfg", "hibernate.cfg.xml"); File baseDir = new File("contents_grids"); DaoFactory f = DaoFactory.createInstance(); DaoContext c = f.getDaoContext(); c.beginTransaction(); try { clearAll(f); List<String> gridIds = setupGrids(f, baseDir); for (String gid : gridIds) { setupUsers(f, gid, baseDir); setupResources(f, gid, baseDir); setupExternalServices(f, gid, baseDir); } } finally { c.commitTransaction(); } }
private static void setupResources(DaoFactory factory, String gridId, File baseDir) throws Exception { baseDir = new File(new File(new File(baseDir, "grids"), gridId), "resources"); ResourceDao dao = factory.createResourceDao(); for (File f : baseDir.listFiles(new RegexFileNameFilter(".*\\.json$"))) { Resource r = new LangridJSON(baseDir, FilenameUtil.getIdFromFileName(f)) .parse(new FileInputStream(f), Resource.class); for (ResourceAttribute a : r.getAttributes()) { a.setGridId(r.getGridId()); a.setResourceId(r.getResourceId()); } dao.addResource(r); } }
private static void setupExternalServices(DaoFactory factory, String gridId, File baseDir) throws Exception { baseDir = new File(new File(new File(baseDir, "grids"), gridId), "services"); ServiceDao dao = factory.createServiceDao(); for (File f : baseDir.listFiles(new RegexFileNameFilter("es_.*\\.json$"))) { ExternalService s = new LangridJSON(baseDir, FilenameUtil.getIdFromServiceFileName(f)) .parse(new FileInputStream(f), ExternalService.class); for (ServiceAttribute a : s.getAttributes()) { a.setGridId(gridId); a.setServiceId(s.getServiceId()); } for (ServiceEndpoint a : s.getServiceEndpoints()) { a.setGridId(gridId); a.setServiceId(s.getServiceId()); } s.setGridId(gridId); dao.addService(s); } }
private static void clearAll(DaoFactory factory) throws Exception { factory.createServiceDao().clear(); factory.createResourceDao().clear(); factory.createUserDao().clear(); factory.createGridDao().clear(); }