|
| 1 | +/* |
| 2 | + * Copyright 2005-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ldap.itest.transaction.compensating.manager; |
| 18 | + |
| 19 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 20 | +import org.springframework.ldap.core.DirContextAdapter; |
| 21 | +import org.springframework.ldap.core.DistinguishedName; |
| 22 | +import org.springframework.ldap.core.LdapTemplate; |
| 23 | +import org.springframework.transaction.annotation.Transactional; |
| 24 | + |
| 25 | +@Transactional |
| 26 | +public class LdapAndJdbcDummyDaoImpl implements DummyDao { |
| 27 | + |
| 28 | + private LdapTemplate ldapTemplate; |
| 29 | + |
| 30 | + private JdbcTemplate jdbcTemplate; |
| 31 | + |
| 32 | + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { |
| 33 | + this.jdbcTemplate = jdbcTemplate; |
| 34 | + } |
| 35 | + |
| 36 | + public void setLdapTemplate(LdapTemplate ldapTemplate) { |
| 37 | + this.ldapTemplate = ldapTemplate; |
| 38 | + } |
| 39 | + |
| 40 | + /* |
| 41 | + * (non-Javadoc) |
| 42 | + * |
| 43 | + * @see |
| 44 | + * org.springframework.ldap.transaction.support.DummyDao#createWithException(java.lang |
| 45 | + * .String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) |
| 46 | + */ |
| 47 | + public void createWithException(String country, String company, String fullname, String lastname, |
| 48 | + String description) { |
| 49 | + create(country, company, fullname, lastname, description); |
| 50 | + throw new DummyException("This method failed"); |
| 51 | + } |
| 52 | + |
| 53 | + /* |
| 54 | + * (non-Javadoc) |
| 55 | + * |
| 56 | + * @see org.springframework.ldap.transaction.support.DummyDao#create(java.lang.String, |
| 57 | + * java.lang.String, java.lang.String, java.lang.String, java.lang.String) |
| 58 | + */ |
| 59 | + public void create(String country, String company, String fullname, String lastname, String description) { |
| 60 | + DistinguishedName dn = new DistinguishedName(); |
| 61 | + dn.add("ou", country); |
| 62 | + dn.add("ou", company); |
| 63 | + dn.add("cn", fullname); |
| 64 | + |
| 65 | + DirContextAdapter ctx = new DirContextAdapter(); |
| 66 | + ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); |
| 67 | + ctx.setAttributeValue("cn", fullname); |
| 68 | + ctx.setAttributeValue("sn", lastname); |
| 69 | + ctx.setAttributeValue("description", description); |
| 70 | + this.ldapTemplate.bind(dn, ctx, null); |
| 71 | + this.jdbcTemplate.update("insert into PERSON values(?, ?, ?)", |
| 72 | + new Object[] { fullname, lastname, description }); |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * (non-Javadoc) |
| 77 | + * |
| 78 | + * @see org.springframework.ldap.transaction.support.DummyDao#update(java.lang.String, |
| 79 | + * java.lang.String, java.lang.String) |
| 80 | + */ |
| 81 | + public void update(String dn, String fullname, String lastname, String description) { |
| 82 | + DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); |
| 83 | + ctx.setAttributeValue("sn", lastname); |
| 84 | + ctx.setAttributeValue("description", description); |
| 85 | + |
| 86 | + this.ldapTemplate.modifyAttributes(ctx); |
| 87 | + this.jdbcTemplate.update("update PERSON set lastname=?, description = ? where fullname = ?", |
| 88 | + new Object[] { lastname, description, fullname }); |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + * (non-Javadoc) |
| 93 | + * |
| 94 | + * @see |
| 95 | + * org.springframework.ldap.transaction.support.DummyDao#updateWithException(java.lang |
| 96 | + * .String, java.lang.String, java.lang.String) |
| 97 | + */ |
| 98 | + public void updateWithException(String dn, String fullname, String lastname, String description) { |
| 99 | + update(dn, fullname, lastname, description); |
| 100 | + throw new DummyException("This method failed."); |
| 101 | + } |
| 102 | + |
| 103 | + /* |
| 104 | + * (non-Javadoc) |
| 105 | + * |
| 106 | + * @see |
| 107 | + * org.springframework.ldap.transaction.support.DummyDao#updateAndRename(java.lang. |
| 108 | + * String, java.lang.String, java.lang.String) |
| 109 | + */ |
| 110 | + public void updateAndRename(String dn, String newDn, String description) { |
| 111 | + DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); |
| 112 | + ctx.setAttributeValue("description", description); |
| 113 | + |
| 114 | + this.ldapTemplate.modifyAttributes(ctx); |
| 115 | + this.ldapTemplate.rename(dn, newDn); |
| 116 | + } |
| 117 | + |
| 118 | + /* |
| 119 | + * (non-Javadoc) |
| 120 | + * |
| 121 | + * @see |
| 122 | + * org.springframework.ldap.transaction.support.DummyDao#updateAndRenameWithException( |
| 123 | + * java.lang.String, java.lang.String, java.lang.String) |
| 124 | + */ |
| 125 | + public void updateAndRenameWithException(String dn, String newDn, String description) { |
| 126 | + updateAndRename(dn, newDn, description); |
| 127 | + throw new DummyException("This method failed."); |
| 128 | + } |
| 129 | + |
| 130 | + /* |
| 131 | + * (non-Javadoc) |
| 132 | + * |
| 133 | + * @see |
| 134 | + * org.springframework.ldap.transaction.support.DummyDao#modifyAttributes(java.lang. |
| 135 | + * String, java.lang.String, java.lang.String) |
| 136 | + */ |
| 137 | + public void modifyAttributes(String dn, String lastName, String description) { |
| 138 | + DirContextAdapter ctx = (DirContextAdapter) this.ldapTemplate.lookup(dn); |
| 139 | + ctx.setAttributeValue("sn", lastName); |
| 140 | + ctx.setAttributeValue("description", description); |
| 141 | + |
| 142 | + this.ldapTemplate.modifyAttributes(dn, ctx.getModificationItems()); |
| 143 | + } |
| 144 | + |
| 145 | + /* |
| 146 | + * (non-Javadoc) |
| 147 | + * |
| 148 | + * @see |
| 149 | + * org.springframework.ldap.transaction.support.DummyDao#modifyAttributesWithException |
| 150 | + * (java.lang.String, java.lang.String, java.lang.String) |
| 151 | + */ |
| 152 | + public void modifyAttributesWithException(String dn, String lastName, String description) { |
| 153 | + modifyAttributes(dn, lastName, description); |
| 154 | + throw new DummyException("This method failed."); |
| 155 | + } |
| 156 | + |
| 157 | + /* |
| 158 | + * (non-Javadoc) |
| 159 | + * |
| 160 | + * @see org.springframework.ldap.transaction.support.DummyDao#unbind(java.lang.String) |
| 161 | + */ |
| 162 | + public void unbind(String dn, String fullname) { |
| 163 | + this.ldapTemplate.unbind(dn); |
| 164 | + this.jdbcTemplate.update("delete from PERSON where fullname=?", new Object[] { fullname }); |
| 165 | + } |
| 166 | + |
| 167 | + /* |
| 168 | + * (non-Javadoc) |
| 169 | + * |
| 170 | + * @see |
| 171 | + * org.springframework.ldap.transaction.support.DummyDao#unbindWithException(java.lang |
| 172 | + * .String) |
| 173 | + */ |
| 174 | + public void unbindWithException(String dn, String fullname) { |
| 175 | + unbind(dn, fullname); |
| 176 | + throw new DummyException("This operation failed."); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void deleteRecursively(String dn) { |
| 181 | + throw new UnsupportedOperationException(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void deleteRecursivelyWithException(String dn) { |
| 186 | + throw new UnsupportedOperationException(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public void createRecursivelyAndUnbindSubnode() { |
| 191 | + throw new UnsupportedOperationException(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void createRecursivelyAndUnbindSubnodeWithException() { |
| 196 | + throw new UnsupportedOperationException(); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments