📄 sync4jengine.java
字号:
} return EngineHelper.generateStatusCommands( operationStatus, msgId, cmdIdGenerator ); } /** * Adds a new client source to the list of the sources the engine deals with. * * @param source the source to be added */ public void addClientSource(SyncSource source) { log.finest("adding " + source); clientSources.add(source); } /** * Returns the client sources * * @return the client sources */ public List getClientSources() { return clientSources; } /** * Returns the client source with the given name if there is any. * * @param name the source name * * @return the client source with the given name if there is any, null otherwise */ public SyncSource getClientSource(String name) { Iterator i = clientSources.iterator(); SyncSource s = null; while (i.hasNext()) { s = (SyncSource)i.next(); if (s.getSourceURI().equals(name)) { return s; } } return null; } /** * Returns the server source with the given name if there is any. * * @param name the source name * * @return the server source with the given name if there is any, null otherwise */ public SyncSource getServerSource(String name) { Iterator i = serverSources.iterator(); SyncSource s = null; while (i.hasNext()) { s = (SyncSource)i.next(); if (s.getSourceURI().equals(name)) { return s; } } return null; } /** * Return the <i>DataStore</i> object corresponding to the given * <i>Database</i> object. * * @param database the database to convert * * @return the <i>DataStore</i> object corresponding to the given * <i>Database</i> object. * */ public DataStore databaseToDataStore(Database db) { ContentTypeInfo contentTypeInfo = new ContentTypeInfo(db.getType(), "-" /* version */); return new DataStore( new SourceRef(db.getSource()) , null , -1 , contentTypeInfo , new ContentTypeInfo[] { contentTypeInfo } , contentTypeInfo , new ContentTypeInfo[] { contentTypeInfo } , new DSMem(false, -1, -1) , new SyncCap(new SyncType[] { SyncType.SLOW }) ); } /** * Returns the content type capabilities of this sync engine. * * @return the content type capabilities of this sync engine. */ public CTCap[] getContentTypeCapabilities() { return new CTCap[0]; } /** * Returns the extensions of this sync engine. * * @return the extensions of this sync engine. */ public Ext[] getExtensions() { return new Ext[0]; } /** * Returns the data store capabilities of the sync sources to synchronize. * * @return the data store capabilities of the sync sources to synchronize. */ public DataStore[] getDatastores() { DataStore[] ds = null; ArrayList al = new ArrayList(); Database db = null; String uri = null; Iterator i = dbs.values().iterator(); while(i.hasNext()) { db = (Database)i.next(); uri = db.getName(); SyncSource ss = getServerSource(uri); if ( ss != null) { al.add(EngineHelper.toDataStore(uri, ss.getInfo())); } } int size = al.size(); if (size == 0) { ds = new DataStore[0]; } else { ds = (DataStore[])al.toArray(new DataStore[size]); } return ds; } /** * Creates and returns a <i>DeviceInfo</i> object with the information * extracted from the configuration object. * * @param verDTD the version of the DTD to use to encode the capabilities * * @return the engine capabilities * * @throws ConfigurationException a runtime exception in case of misconfiguration */ public DevInf getServerCapabilities(VerDTD verDTD) { DevInf devInf = new DevInf( verDTD, configuration.getStringValue(CFG_ENGINE_MANIFACTURER, "Sync4j"), configuration.getStringValue(CFG_ENGINE_MODELNAME, "Sync4j"), configuration.getStringValue(CFG_ENGINE_OEM, null ), configuration.getStringValue(CFG_ENGINE_FWVERSION, null ), configuration.getStringValue(CFG_ENGINE_SWVERSION, null ), configuration.getStringValue(CFG_ENGINE_HWVERSION, null ), configuration.getStringValue(CFG_ENGINE_DEVICEID, "Sync4j"), configuration.getStringValue(CFG_ENGINE_DEVICETYPE, "Server"), getDatastores(), getContentTypeCapabilities(), getExtensions(), false, false, false ); return devInf; } /** * First of all, check the availablity and accessibility of the given * databases. The state of the given database will change as described below * (and in the same order): * <ul> * <li>The database status code is set to <i>StatusCode.OK</i> if the * database is accessible, <i>StatusCode.NOT_FOUND</i> if the database * is not found and <i>StatusCode.FORBIDDEN</i> if the principal is * not allowed to access the database. * <li>If the currently set last anchor does not match the last tag * retrieved from the database (DBMS) and the requested alert code is * not a refresh, the synchronization method is set to * <i>AlertCode.SLOW</i> * <li>The database server sync anchor is set to the server-side sync anchor * * @param principal the principal the is requesting db preparation * @param dbs an array of <i>Database</i> objects - NULL * @param next the sync timestamp of the current synchronization * */ public void prepareDatabases(Sync4jPrincipal principal, Database[] dbs , SyncTimestamp next ) { for (int i=0; ((dbs != null) && (i < dbs.length)); ++i) { int statusCode = StatusCode.OK; if (!checkServerDatabase(dbs[i])) { statusCode = StatusCode.NOT_FOUND; } else if (!checkDatabasePermissions(dbs[i])) { statusCode = StatusCode.FORBIDDEN; } dbs[i].setStatusCode(statusCode); // // Now retrieve the last sync anchor // if (statusCode == StatusCode.OK) { LastTimestamp last = new LastTimestamp( principal.getId(), dbs[i].getName() ); try { store.read(last); dbs[i].setServerAnchor(new Anchor(last.tag, next.tag)); } catch (NotFoundException e) { // // No prev last sync! Create a new anchor that won't match // last.tag = next.tag; dbs[i].setServerAnchor(new Anchor(next.tag, next.tag)); } catch(PersistentStoreException e) { log.severe("Unable to retrieve timestamp from store"); log.throwing(getClass().getName(), "prepareDatabases", e); } if ( !(last.tag.equals(dbs[i].getAnchor().getLast())) && (dbs[i].getMethod() != AlertCode.REFRESH_FROM_SERVER)) { if (log.isLoggable(Level.FINE)) { log.fine( "Forcing slow sync for database " + dbs[i].getName() ); log.fine( "Server last: " + last.tag + "; client last: " + dbs[i].getAnchor().getLast() ); } dbs[i].setMethod(AlertCode.SLOW); } } } } /** * Authenticates the given credential using <i>officer</i>. * * @param credential the credential to be authenticated * * @return true if the credential is authenticated, false otherwise */ public boolean login(Cred credential) { return officer.authenticate(credential); } public boolean isGuestEnabled() { return officer.isGuestEnabled(); } /** * Logs out the given credential using <i>officer</i>. * * @param credential the credential to be authenticated * * @return true if the credential is authenticated, false otherwise */ public void logout(Cred credential) { officer.unAuthenticate(credential); } /** * Authorizes the given principal to access the given resource using * <i>officer</i>. * * @param principal the entity to be authorized * @param resource the name of the resource * * @return true if the principal is authorized, false otherwise */ public boolean authorize(Principal principal, String resource) { return officer.authorize(principal, resource); } /** * Is the server in debug mode? * * @return the value of the configuration property CFG_SERVER_DEBUG */ public boolean isDebug() { return configuration.getBoolValue(CFG_SERVER_DEBUG, false); } /** * Translates an array of <i>SyncOperation</i> objects to an array of * <i>(Add,Delete,Replace)Command</i> objects. Only client side operations * are translated. * * @param clientMapping the associated existing client mapping * @param operations the operations to be translated * @param sourceName the corresponding source name * * @return the sync4j commands corresponding to <i>operations</i> */ public ItemizedCommand[] operationsToCommands(ClientMapping clientMapping, SyncOperation[] operations , String sourceName ) { return EngineHelper.operationsToCommands( clientMapping , operations , sourceName , cmdIdGenerator); } /** * Updates the mapping given an array of operations. This is used for * mappings sent by the client. * * @param clientMappings the client mappings for current synchronization * @param operations the operation performed * */ public void updateClientMappings(Map clientMappings, SyncOperation[] operations , boolean slowSync ) { try { EngineHelper.updateClientMappings(clientMappings, operations, slowSync); } catch (Exception e) { log.throwing(getClass().getName(), "updateClientMappings", e); } } /** * Updates the LUID-GUIDmapping for the client modifications (that is the * items directlry inserted or removed by the server). * * @param clientMappings the client mappings for current synchronization * */ public void updateServerMappings(Map clientMappings, boolean slowSync) { try { EngineHelper.updateServerMappings(clientMappings, operationStatus, slowSync); } catch (Exception e) { log.throwing(getClass().getName(), "updateServerMappings", e); } } /** * Converts an array of <i>Item</i> objects belonging to the same * <i>SyncSource</i> into an array of <i>SyncItem</i> objects. * <p> * The <i>Item</i>s created are enriched with an additional property * called as in SyncItemHelper.PROPERTY_COMMAND, which is used to bound the * newly created object with the original command. * * @param syncSource the <i>SyncSource</i> items belong to - NOT NULL * @param items the <i>Item</i> objects * @param state the state of the item as one of the values defined in * <i>SyncItemState</i> * @param the timestamp to assign to the last even on this item * * * @return an array of <i>SyncItem</i> objects */ public static SyncItem[] itemsToSyncItems(ClientMapping clientMapping, SyncSource syncSource , ModificationCommand cmd , char state , long timestamp ) { return EngineHelper.itemsToSyncItems(clientMapping, syncSource, cmd, state, timestamp); } // --------------------------------------------------------- Private methods /** /** * Checks if the given database is managed by this server. * * @param db the database to be checked * * @return true if the database is under control of this server, false otherwise * */ private boolean checkServerDatabase(Database db) { if (log.isLoggable(Level.FINEST)) { log.finest( "Checking if the database " + db + " is in the server database list " + serverSources ); } SyncSource source = null; Iterator i = serverSources.iterator(); while(i.hasNext()) { source = (SyncSource)i.next(); if (db.getName().equals(source.getSourceURI())) { log.finest("Yes sir!"); return true; } } log.finest("Not found sir"); // // not found... // return false; } /** * Checks if the principal associated to the database is allowed to synchronize.<br> * * @param db the database to be checked * * @return true if the credential is allowed to access the database, false otherwise */ private boolean checkDatabasePermissions(Database db) { return authorize(db.getPrincipal(), db.getName()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -